473,583 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ 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 3874
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.Unicod e.GetString(byt es);
(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.co m>
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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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.Unicod e.GetString(byt es);


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.UT F8.GetBytes(tex tBox1.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.G etString(binary Data2);

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.Unicod e.GetString(byt es);


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.UT F8.GetBytes(tex tBox1.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.AppendF ormat ("/0x{0:x2}", b);
}
string coded = builder.ToStrin g();

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.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.AppendF ormat ("/0x{0:x2}", b);
}
string coded = builder.ToStrin g();


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
1931
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 to display Polish accent characters and here is a problem. On my testing/developing server (Windows 2003 Server) all works fine, but on production...
3
5461
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 “\u1234”) It seems to work the first time I set the button to the Unicode character. After a while, when saving my code, I get a pop-up window stating...
40
3194
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, including asian languages (chinese, japanese, korean, etc). First of all, strings will be passed to my class methods, some of which based on the...
0
7824
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8176
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8321
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
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 we have to send another system
0
1154
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.