Open In App

Tensorflow.js tf.encodeString() Function

Last Updated : 14 Aug, 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 .encodeString() function is used to encode the stated string into bytes with the help of the given encoding scheme.

Syntax :  

tf.encodeString(s, encoding?)

Parameters:  

  • s: It is the stated string which is to be encoded. It is of type string.
  • encoding: It is the encoding scheme which is to be used. And the by default value is utf-8.

Return Value: It returns Uint8Array.

Example 1: When encoding scheme is not provided.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Calling tf.encodeString() method and
// printing output
console.log(tf.util.encodeString("fghi"));


 Output:

102,103,104,105

Example 2: When encoding scheme is provided.

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Defining string and encoding
// scheme
var str = "1234"
var encoding = "utf-8"
 
// Calling tf.encodeString() method and
// printing output
var x = tf.util.encodeString(str, encoding);
console.log(x);


Output: 

49,50,51,52

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


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

Similar Reads