displaying unicode encodings (ie 0x73 type notation)

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

I'm trying to write a basic tool to convert strings to unicode
encodings. Should be easy enough, I can do the encoding bit with the
various encoding tools in C#, but what I can't seem to do is force C#
to spit out the encodings - everything I've done just seems to decode
the unicode and spit out exactly what I had typed in. How do I go from
a byte[] of unicode bytes to a string?

(As you might guess, I'm not really much of a coder - even when I was,
dealing with characters was the part of coding I hated. Gimme numbers
any day...)

Greg
 
Greg said:
I'm trying to write a basic tool to convert strings to unicode
encodings. Should be easy enough, I can do the encoding bit with the
various encoding tools in C#, but what I can't seem to do is force C#
to spit out the encodings - everything I've done just seems to decode
the unicode and spit out exactly what I had typed in. How do I go from
a byte[] of unicode bytes to a string?

Use Encoding.Unicode.GetString(bytes);
(As you might guess, I'm not really much of a coder - even when I was,
dealing with characters was the part of coding I hated. Gimme numbers
any day...)

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 
Jon Skeet said:
Greg said:
I'm trying to write a basic tool to convert strings to unicode
encodings. Should be easy enough, I can do the encoding bit with the
various encoding tools in C#, but what I can't seem to do is force C#
to spit out the encodings - everything I've done just seems to decode
the unicode and spit out exactly what I had typed in. How do I go from
a byte[] of unicode bytes to a string?

Use Encoding.Unicode.GetString(bytes);

This was what I had originally attempted, but it just decodes the
Unicode - what I want is to write "test" into a textbox, and then get
something like
/0x74/0x65/0x73/0x74 in a second textbox. When I do this:

binaryData2 = UTF8Encoding.UTF8.GetBytes(textBox1.Text);

binaryData2 is loaded with exactly what I'd like to print out, but I'm
not sure how to get it into a format that a textbox will accept. Doing
what you suggested:

textBox2.Text = Encoding.UTF8.GetString(binaryData2);

Just gives me back the string "test." I suppose if there's nothing to
do this, what I need to do is figure out how to either push bytes into
a textbox, or how to convert bytes to chars.

Thanks for that - I'll have a go.

Greg
 
Greg said:
This was what I had originally attempted, but it just decodes the
Unicode - what I want is to write "test" into a textbox, and then get
something like
/0x74/0x65/0x73/0x74 in a second textbox. When I do this:

binaryData2 = UTF8Encoding.UTF8.GetBytes(textBox1.Text);

In that case what you're *really* asking is how to convert the byte
array {0x74, 0x65, 0x73, 0x74} into the string
"/0x74/0x65/0x73/0x74", which fortunately has nothing to do with the
nasty business of actual decoders.

Something like this will do it though:

StringBuilder builder = new StringBuilder();
foreach (byte b in myByteArray)
{
builder.AppendFormat ("/0x{0:x2}", b);
}
string coded = builder.ToString();
 
Jon Skeet said:
In that case what you're *really* asking is how to convert the byte
array {0x74, 0x65, 0x73, 0x74} into the string
"/0x74/0x65/0x73/0x74", which fortunately has nothing to do with the
nasty business of actual decoders.

Yeah, that seems prettty accurate. Shame I didn't ask that first, eh? :)
Something like this will do it though:

StringBuilder builder = new StringBuilder();
foreach (byte b in myByteArray)
{
builder.AppendFormat ("/0x{0:x2}", b);
}
string coded = builder.ToString();

Excellent stuff - many thanks for that Jon!
 
Back
Top