StringToByteArray["string"]
returns a byte array corresponding to the UTF-8 encoding of the specified string.
StringToByteArray["string","encoding"]
uses the specified character encoding.
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
- The result of StringToByteArray["string",…] is a ByteArray object.
- The value of "encoding" can be any member of $CharacterEncodings.
- StringToByteArray["string","encoding"] requires that all characters in "string" have a character code in "encoding".
Examples
open all close allBasic Examples (2)
Convert a string to a ByteArray of its UTF-8 representation:
StringToByteArray["aAα"]Convert the bytes to a list of numbers:
Normal[%]Convert a string to a ByteArray of its ISO 8859-7 Latin/Greek representation:
StringToByteArray["aAα", "ISO8859-7"]Convert the bytes to a list of numbers:
Normal[%]Scope (2)
Convert a generated string to UTF-8:
StringToByteArray[RandomWord[]]Compare different character encodings:
str = "ÁÉÍÓÚ";StringToByteArray[str, "ISO8859-1"]StringToByteArray[str, "UTF-8"]For these particular characters, ISO8859-15 is identical to ISO8859-1:
StringToByteArray[str, "ISO8859-1"] == StringToByteArray[str, "ISO8859-15"]Properties & Relations (7)
In UTF-8, the number of bytes equals the length of the string only for all-ASCII strings:
allASCII = StringJoin[CharacterRange[0, 127]];
Length[StringToByteArray[allASCII]] == StringLength[allASCII]The next 1920 characters each require 2 bytes:
str = StringJoin[CharacterRange[128, 2047]];
Length[StringToByteArray[str]] == 2StringLength[str]And the remaining 63488 code points each require 3 bytes:
str = StringJoin[CharacterRange[2048, 65535]];
Length[StringToByteArray[str]] == 3StringLength[str]The characters in code points 0 through 255 can be represented in ISO8859-1:
str = StringJoin[CharacterRange[0, 255]];StringToByteArray[str, "ISO8859-1"]Each character is encoded as a byte containing the code point:
Normal[%] == ToCharacterCode[str]In the UTF-8 encoding, the size of byte array object returned is essentially the size of original string:
text = ExampleData[{"Text", "BeowulfOldEnglish"}];
ByteCount[text]ByteCount[StringToByteArray[text]]StringToByteArray[str] converts each character to a sequence of one or more byte values:
StringToByteArray["aα"]%//NormalToCharacterCode[str] returns a list of integer code points:
ToCharacterCode["aα"]An empty string produces an empty byte array:
StringToByteArray[""]In a given character encoding, StringToByteArray and ToCharacterCode return the same bytes:
text = ExampleData[{"Text", "DonQuixoteIEnglish"}];
ba = StringToByteArray[text];
byteList = ToCharacterCode[text, "UTF-8"];
byteList === Normal[ba]The list returned by ToCharacterCode uses several times more memory:
{ByteCount[ba], ByteCount[byteList]}The ByteArray object returned by StringToByteArray uses essentially one byte of memory per value:
numOfValues = Length[ba];
N[ByteCount[ba] / numOfValues]The list returned by ToCharacterCode uses several bytes of memory per value:
N[ByteCount[byteList] / numOfValues]StringToByteArray is the inverse of ByteArrayToString:
ba = ByteArray[Range[10]];
ba == StringToByteArray[ByteArrayToString[ba]]See Also
ByteArrayToString ToCharacterCode $CharacterEncodings BaseDecode ExportByteArray ByteArray String
Formats: Byte
Related Guides
History
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_2026_stringtobytearray, author="Wolfram Research", title="{StringToByteArray}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/StringToByteArray.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringtobytearray, organization={Wolfram Research}, title={StringToByteArray}, year={2017}, url={https://reference.wolfram.com/language/ref/StringToByteArray.html}, note=[Accessed: 13-June-2026]}