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 allclose allBasic Examples (2)
Scope (2)
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:
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