Open In App

Tensorflow.js tf.decodeString() Function

Last Updated : 18 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  • bytes: It is the stated bytes which is to be decoded. It is of type Uint8Array.
  • encoding: It is the encoding scheme which is to be used. And the by default value is utf-8.

Return Value: It returns string.

Example 1: When encoding scheme is provided.

Javascript




// 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.

Javascript




// 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads