StringToByteArray

StringToByteArray["string"]

returns a byte array corresponding to the UTF-8 encoding of the specified string.

StringToByteArray["string","encoding"]

uses the specified character encoding.

Details

Examples

open allclose all

Basic Examples  (2)

Convert a string to a ByteArray of its UTF-8 representation:

Convert the bytes to a list of numbers:

Convert a string to a ByteArray of its ISO 8859-7 Latin/Greek representation:

Convert the bytes to a list of numbers:

Scope  (2)

Convert a generated string to UTF-8:

Compare different character encodings:

For these particular characters, ISO8859-15 is identical to ISO8859-1:

Properties & Relations  (6)

In UTF-8, the number of bytes equals the length of the string only for all-ASCII strings:

The next 1920 characters each require 2 bytes:

And the remaining 63488 code points each require 3 bytes:

The characters in code points 0 through 255 can be represented in ISO8859-1:

Each character is encoded as a byte containing the code point:

In the UTF-8 encoding, the size of byte array object returned is essentially the size of original string:

StringToByteArray[str] converts each character to a sequence of one or more byte values:

ToCharacterCode[str] returns a list of integer code points:

In a given character encoding, StringToByteArray and ToCharacterCode return the same bytes:

The list returned by ToCharacterCode uses several times more memory:

The ByteArray object returned by StringToByteArray uses essentially one byte of memory per value:

The list returned by ToCharacterCode uses several bytes of memory per value:

StringToByteArray is the inverse of ByteArrayToString:

Possible Issues  (2)

All characters in the string must be representable in the character encoding:

A zero-length string produces an empty list:

Introduced in 2017
 (11.2)