({{, (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])[ba] 
returns a string by decoding the data in the byte array ba, assuming UTF-8 encoding.}, {, (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])[ba,"encoding"]
interprets the data in the specified character encoding.}})

OpenersNotesSection(Spacer1)Details

In (RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])[ba,…], ba should be 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".

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Create a string from a (RefLink[ByteArray, paclet:ref/ByteArray]), assuming it contains UTF-8 data:

ByteArray[{97,65,206,177}]

ByteArrayToString[%]

Create a string from a (RefLink[ByteArray, paclet:ref/ByteArray]), interpreting the data as ISO 8859-7 Latin/Greek:

ByteArray[{97,65,225}]

ByteArrayToString[%,"ISO8859-7"]

OpenersExampleSection1(Spacer1)Scope  (2)

Convert ASCII bytes of English capital letters to a string:

ascii=ByteArray[RandomInteger[ToCharacterCode["AZ"],10]]

ByteArrayToString[ascii]

Compare different character encodings:

highRange=ByteArray[Range[191,200]]

ByteArrayToString[highRange,"ISO8859-1"]

ByteArrayToString[highRange,"ISO8859-5"]

OpenersExampleSection3(Spacer1)Applications  (1)

Import a webpage:

data=URLRead["mbl.is"]

Extract the bytes of the page in a (RefLink[ByteArray, paclet:ref/ByteArray]) object:

bytes=data["BodyByteArray"]

Convert the UTF-8 encoded data to a string:

str=ByteArrayToString[bytes];

Display the first five lines of the raw page:

Snippet[str,5]

OpenersExampleSection4(Spacer1)Properties & Relations  (4)

All bytes can be represented in ISO8859-1:

ba=ByteArray[Range[0,255]]

ByteArrayToString[ba,"ISO8859-1"]//InputForm

The characters produced in this encoding have code points 0 to 255:

ToCharacterCode[%]==Range[0,255]

The length of the string generated is less than or equal to the length of the byte array:

ba=ByteArray[{215,169,215,156,215,149,215,157,32,104,101,108,108,111}]

StringLength[ByteArrayToString[ba]]

StringLength[ByteArrayToString[ba,"ISO8859-8"]]

(RefLink[ByteArrayToString, paclet:ref/ByteArrayToString])[str] decodes UTF-8 bytes to a string representation:

bl={195,139,65,195,146,75,195,135,120,195,129,115,195,137,93};
ba=ByteArray[bl];
ByteArrayToString[ByteArray[ba]]

(RefLink[FromCharacterCode, paclet:ref/FromCharacterCode])[bytes] converts code points to a string:

FromCharacterCode[bl]

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

str="I will round trip";
str==ByteArrayToString[StringToByteArray[str]]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

Not all byte sequences are valid UTF-8 codes:

str=ByteArrayToString[ByteArray[{126,162,195, 136}]]

Invalid bytes—in this case 162—are interpreted as literal code points:

ToCharacterCode[str]

Converting the string back to a byte array produces a different but valid UTF-8 byte sequence:

StringToByteArray[str]//Normal

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[StringToByteArray, paclet:ref/StringToByteArray])  ▪  (RefLink[FromCharacterCode, paclet:ref/FromCharacterCode])  ▪  (RefLink[$CharacterEncodings, paclet:ref/$CharacterEncodings])  ▪  (RefLink[BaseEncode, paclet:ref/BaseEncode])  ▪  (RefLink[ImportByteArray, paclet:ref/ImportByteArray])  ▪  (RefLink[ByteArray, paclet:ref/ByteArray])  ▪  (RefLink[String, paclet:ref/String])}})}})

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