To convert a string to utf-8 encoding in javascript, you can use the TextEncoder
API. Here’s an example:
index.tsx305 chars12 lines
In the above code, we first create a new TextEncoder
instance which we’ll use to encode the string. We then use the .encode()
method of the TextEncoder
to convert the string to a Uint8Array
array of bytes in utf-8 encoding.
To convert the utf-8 buffer back to a string, we create a new TextDecoder
instance and use the .decode()
method to get the utf-8 encoded string.
Note: The TextEncoder
and TextDecoder
APIs are not supported by some older browsers. So make sure to check browser compatibility before using these APIs in your code.
gistlibby LogSnag