473,473 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

displaying unicode encodings (ie 0x73 type notation)

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
Nov 15 '05 #1
4 3865
Greg <gr****@hush.ai> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Greg <gr****@hush.ai> wrote:
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.
See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.


Thanks for that - I'll have a go.

Greg
Nov 15 '05 #3
Greg <gr****@hush.ai> wrote:
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);


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
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!

--
Greg
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ziemowit Skowronski | last post by:
Hi, I have a problem with displaying unicode strings in ASP.NET (C#). I recieve data from MS SQL Server database and use them to construct navigation on the page. Because I'm from Poland, I have...
3
by: Kidus Yared | last post by:
I am having a problem displaying Unicode characters on my Forms labels and buttons. After coding Button1.Text = unicode; where the unicode is a Unicode character or string (‘\u1234’ or...
40
by: apprentice | last post by:
Hello, I'm writing an class library that I imagine people from different countries might be interested in using, so I'm considering what needs to be provided to support foreign languages,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.