I have a string that I need to convert to the equivalent array of bytes in .NET.
This ought to be easy, but I am having a brain cramp.
|
You need to use an encoding to tell .NET what you expect as the output. For example, in UTF-16:
|
|||||||||||||
|
Like this:
|
|||
|
What Encoding are you using? Konrad's got it pretty much down, but there are others out there and you could get goofy results with the wrong one:
Where
|
|||
|
First work out which encoding you want: you need to know a bit about Unicode first. Next work out which Finally, work out whether you want all the bytes at once (which is the easiest way of working - call Encoding.GetBytes(string) once and you're done) or whether you need to break it into chunks - in which case you'll want to use Encoding.GetEncoder and then encode a bit at a time. The encoder takes care of keeping the state between calls, in case you need to break off half way through a character, for example. |
|||||||||||||||||||||
|