How do I convert a string
to a byte[]
in .NET (C#)?
Also, why should encoding be taken into consideration? Can't I simply get what bytes the string has been stored in? Why is there a dependency on character encodings?
|
A character is both a lookup key into a font table and a lexical tradition such as ordering, upper and lower case versions, etc. Consequently, a character is not a byte (8-bits) and a byte is not a character. In particular, the 256 permutations of a byte cannot accommodate the thousands of symbols within some written languages, much less all languages. Hence, various methods for encoding characters have been devised. Some encode for a particular class of languages (ASCII encoding); multiple languages using code pages (Extended ASCII); or, ambitiously, all languages by selectively including additional bytes as needed, Unicode. Within a system, such as the .Net framework, a String implies a particular character encoding. In .Net this encoding is Unicode. Since the framework reads and writes Unicode by default, dealing with character encoding is typically not necessary in .Net. However, in general, to load a character string into the system from a byte stream you need to know the source encoding to therefore interpret and subsequently translate it correctly (otherwise the codes will be taken as already being in the system's default encoding and thus render gibberish). Similarly, when a string is written to an external source, it will be written in a particular encoding. |
||||
|
If you really want a copy of the underlying bytes of a string, you can use a function like the one that follows. However, you shouldn't please read on to find out why.
This function will get you a copy of the bytes underlying your string, pretty quickly. You'll get those bytes in whatever way they are encoding on your system. This encoding is almost certainly UTF-16LE but that is an implementation detail you shouldn't have to care about. It would be safer, simpler and more reliable to just call,
In all likelihood this will give the same result, is easier to type, and the bytes will always round-trip with a call to
|
|||
|
OP's question: "How do I convert a You can use the following code:
As a benefit, encoding does not matter! Oh wait, this is an ecoding... it's just trivial and highly lossy. |
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?