Open In App

Tensorflow.js tf.decodeString() Function

Tensorflow.js is an open-source library which is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .decodeString() function is used to decode the stated bytes into a string with the help of the given encoding scheme.



Syntax :  

tf.decodeString(bytes, encoding?)

Parameters:  



Return Value: It returns string.

Example 1: When encoding scheme is provided.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining array ArrayBuffer
var buffer = new ArrayBuffer(13);
  
// Defining bytes to be decoded
var y = new Uint8Array(buffer);
y[1] = 89;
  
// Calling tf.decodeString() method and
// printing output
const str = tf.util.decodeString(y, "ASCII");
console.log(str);

Output:

Y

Example 2: When encoding scheme is not provided.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining array ArrayBuffer
var buffer = new ArrayBuffer(13);
  
// Defining bytes to be decoded
var y = new Uint8Array(buffer);
y[1] = 75;
  
// Calling tf.decodeString() method and
// printing output
console.log(tf.util.decodeString(y));

Output:

K

Reference: https://js.tensorflow.org/api/latest/#decodeString

Article Tags :