JavaScript

tf.util.encodeString() and tf.util.decodeString() Functions in Tensorflow.js

If you want to encode the string into bytes and vice versa in the Tensorflow.js library, then the tf.encodeString() and tf.decodeString() functions are used.

In this article, we will explore different ways to encode and decode the string using the tf.encodeString() and tf.decodeString().

Tensorflow.js – tf.util.encodeString() Function

tf.encodeString() is used to encode all the characters present in the string into bytes using an encoding format. By default, it encodes using utf-8 encoding format.

UTF- 8 follows the ASCII table, so it encodes each character to its ASCII Values.

Syntax

tf.util.encodeString(actual_string,endoding_format)

It takes two parameters.

Parameters

  1. The actual_string is the string
  2. The encding_format is the format in which the string is encoded. By default, it is utf-8.

Example 1
We will encode the string: ‘Linux Hint’ with utf-8 encoding technique.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Tensorflow.js - tf.util.encodeString()</h1></center>
<script>
//create a string
let actual_string = 'Linux Hint';

//actual tensor
document.write("<b>Actual String: </b>",actual_string);

document.write("<br>");
//encode the string
document.write("<b>Encoded String: </b>"+tf.util.encodeString(actual_string,'utf-8'));
</script>

</body>
</html>

Output

A string is encoded using the utf-8 format.

Example 2
We will encode the string: ‘Linux Hint hold java and other tutorials’ with utf-8 encoding technique.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Tensorflow.js - tf.util.encodeString()</h1></center>
<script>
//create a string
let actual_string = 'Linux Hint hold java and other tutorials';

//actual tensor
document.write("<b>Actual String: </b>",actual_string);

document.write("<br>");
//encode the string
document.write("<b>Encoded String: </b>"+tf.util.encodeString(actual_string,'utf-8'));
</script>

</body>
</html>

Output

A string is encoded using the utf-8 format.

Tensorflow.js – tf.util.decodeString() Function

The tf.decodeString() is used to decode the byte into character using the decoding in ASCII format.

Syntax

tf.util.decodeString(actual_byte,’ASCII’)

It takes two parameters.

Parameters

  1. The actual_byte is the byte.
  2. ASCII converts the byte into a character as an ASCII value.

We need to create an array buffer to store bytes.

Example 1
We will decode the byte – 65 with the tf.util.decodeString() function.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Tensorflow.js - tf.util.decodeString()</h1></center>
<script>
//create a buffer with size 1
let store = new ArrayBuffer(1);

// specify the bytes into the buffer store
let value = new Uint8Array(store);

//add byte
value[0] = 65;

//decode the byte
document.write("Decoded: "+ tf.util.decodeString(value, "ASCII"));
</script>

</body>
</html>

Output

The 65 bytes character is A.

Example 2
We will decode the bytes, 67 and 68, with the tf.util.decodeString() function.

<html>
<!--   CDN Link that delivers the Tensorflow.js framework -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>
<center><h1>Tensorflow.js - tf.util.decodeString()</h1></center>
<script>
//create a buffer with size 2
let store = new ArrayBuffer(2);

// specify the bytes into the buffer store
let value = new Uint8Array(store);

//add byte
value[0] = 67;
value[1] = 68;

//decode the byte
document.write("Decoded: "+ tf.util.decodeString(value, "ASCII"));
</script>

</body>
</html>

Output

The 67 bytes character is C, and 68 is converted to D.

Conclusion

In this article, we saw how to encode and decode the string using the tf.util.encodeString() and tf.util.decodeString() functions in Tensorflow.js.

The tf.uitl.encodeString() takes the utf-8 encoding technique that converts to bytes per ASCII values and tf.uitl.decodeString() takes the ASCII decoding technique that converts to string/character per ASCII values. Make sure you use an array buffer to store bytes for decoding.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain