({{, (RefLink[StringToByteArray, paclet:ref/StringToByteArray])["string"] 
returns a byte array corresponding to the UTF-8 encoding of the specified string.}, {, (RefLink[StringToByteArray, paclet:ref/StringToByteArray])["string","encoding"]
uses the specified character encoding.}})

OpenersNotesSection(Spacer1)Details

The result of (RefLink[StringToByteArray, paclet:ref/StringToByteArray])["string",…] is a (RefLink[ByteArray, paclet:ref/ByteArray]) object.

The value of "encoding" can be any member of (RefLink[$CharacterEncodings, paclet:ref/$CharacterEncodings]), with the exception of "Unicode" or "ISO10646-1".

(RefLink[StringToByteArray, paclet:ref/StringToByteArray])["string","encoding"] requires that all characters in "string" have a character code in "encoding".

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Convert a string to a (RefLink[ByteArray, paclet:ref/ByteArray]) of its UTF-8 representation:

StringToByteArray["aAα"]

Convert the bytes to a list of numbers:

Normal[%]

Convert a string to a (RefLink[ByteArray, paclet:ref/ByteArray]) of its ISO 8859-7 Latin/Greek representation:

StringToByteArray["aAα","ISO8859-7"]

Convert the bytes to a list of numbers:

Normal[%]

OpenersExampleSection1(Spacer1)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"]

OpenersExampleSection3(Spacer1)Properties & Relations  (6)

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]]

(RefLink[StringToByteArray, paclet:ref/StringToByteArray])[str] converts each character to a sequence of one or more byte values:

StringToByteArray["aα\[FormalA]"]

%//Normal

(RefLink[ToCharacterCode, paclet:ref/ToCharacterCode])[str] returns a list of integer code points:

ToCharacterCode["aα\[FormalA]"]

In a given character encoding, (RefLink[StringToByteArray, paclet:ref/StringToByteArray]) and (RefLink[ToCharacterCode, paclet:ref/ToCharacterCode]) return the same bytes:

text=ExampleData[{"Text", "DonQuixoteIEnglish"}];
ba=StringToByteArray[text];
byteList=ToCharacterCode[text,"UTF-8"];
byteList===Normal[ba]

The list returned by (RefLink[ToCharacterCode, paclet:ref/ToCharacterCode]) uses several times more memory:

{ByteCount[ba],ByteCount[byteList]}

The (RefLink[ByteArray, paclet:ref/ByteArray]) object returned by (RefLink[StringToByteArray, paclet:ref/StringToByteArray]) uses essentially one byte of memory per value:

numOfValues=Length[ba];
N[ByteCount[ba]/numOfValues]

The list returned by (RefLink[ToCharacterCode, paclet:ref/ToCharacterCode]) uses several bytes of memory per value:

N[ByteCount[byteList]/numOfValues]

(RefLink[StringToByteArray, paclet:ref/StringToByteArray]) is the inverse of (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString]):

ba=ByteArray[Range[10]];
ba==StringToByteArray[ByteArrayToString[ba]]

OpenersExampleSection4(Spacer1)Possible Issues  (2)

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

StringToByteArray["aα","ISO8859-1"]

A zero-length string produces an empty list:

StringToByteArray[""]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])  ▪  (RefLink[ToCharacterCode, paclet:ref/ToCharacterCode])  ▪  (RefLink[$CharacterEncodings, paclet:ref/$CharacterEncodings])  ▪  (RefLink[BaseDecode, paclet:ref/BaseDecode])  ▪  (RefLink[ExportByteArray, paclet:ref/ExportByteArray])  ▪  (RefLink[ByteArray, paclet:ref/ByteArray])  ▪  (RefLink[String, paclet:ref/String])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Systems-Related String Operations, paclet:guide/SystemsRelatedStringOperations])}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Special Characters, paclet:guide/SpecialCharacters])}, {▪(RefLinkPlain[Cryptography, paclet:guide/Cryptography])}, {▪(RefLinkPlain[Character Operations, paclet:guide/CharacterOperations])}, {▪(RefLinkPlain[Binary Data, paclet:guide/BinaryData])}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}})}})