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  (7)

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:

An empty string produces an empty byte array:

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  (1)

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

Wolfram Research (2017), StringToByteArray, Wolfram Language function, https://reference.wolfram.com/language/ref/StringToByteArray.html.

Text

Wolfram Research (2017), StringToByteArray, Wolfram Language function, https://reference.wolfram.com/language/ref/StringToByteArray.html.

CMS

Wolfram Language. 2017. "StringToByteArray." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/StringToByteArray.html.

APA

Wolfram Language. (2017). StringToByteArray. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringToByteArray.html

BibTeX

@misc{reference.wolfram_2024_stringtobytearray, author="Wolfram Research", title="{StringToByteArray}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/StringToByteArray.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_stringtobytearray, organization={Wolfram Research}, title={StringToByteArray}, year={2017}, url={https://reference.wolfram.com/language/ref/StringToByteArray.html}, note=[Accessed: 22-December-2024 ]}