473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unicode In C#?

Hello,

I was wondering if anybody knew much about Unicode strings in C#.

I have a Unicode encoded string but how can I see the contents of this
string so it doesn't display "???????????"?

I don't want to trans form it back into a ASCII string using Encoding.ASCII
before I view the data its the Unicode data I wish to view.

Thanks

Mark Relly
Nov 15 '05 #1
8 25623
Mark Relly <ma**@mslnet.co.uk> wrote:
I was wondering if anybody knew much about Unicode strings in C#.
There's always more to learn, but I know a fair amount :)
I have a Unicode encoded string but how can I see the contents of this
string so it doesn't display "???????????"?

I don't want to trans form it back into a ASCII string using Encoding.ASCII
before I view the data its the Unicode data I wish to view.


If you want to see the exact contents, I suggest you do something like:

foreach (char x in myString)
{
Console.WriteLine ((int)x);
}

and then lookup what those characters are on http://www.unicode.org

--
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Mark,

When you say "seeing the contents of this string", did you mean printing
it using Console.WriteLine() or something? Because this works just fine
for me (that is, it prints the PI symbol):

~ string Pi = "\u03a0";
~ Console.WriteLine(Pi);

How did you display that string of yours?

Mark Relly wrote:

| Hello,
|
| I was wondering if anybody knew much about Unicode strings in C#.
|
| I have a Unicode encoded string but how can I see the contents of this
| string so it doesn't display "???????????"?
|
| I don't want to trans form it back into a ASCII string using
Encoding.ASCII
| before I view the data its the Unicode data I wish to view.
|
| Thanks
|
| Mark Relly
|
|
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oO5MwEwccQ4rWPgRAnriAJ9a6FjD/9v8gqMQyHJnRaeDMJfzkgCfbbJF
AfgF/owf+LJiJpgO49rNae0=
=l3YE
-----END PGP SIGNATURE-----

Nov 15 '05 #3
When I say seeing the contents of the string yes I do mean using
System.WriteLine.

The code below
challenge = Encoding.Unicode.GetString(byteArray,0,byteArray.L ength);

Console.WriteLine(challenge);

Will give me the output "???????????????????????"

Which is unfortunate as I cannot "see the contents" in a usefull way

Regards

Mark
Nov 15 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

But what is the content of the byteArray, exactly? I mean, when I did this:

~ char[] charArray = "Hello World!\u03a0".ToCharArray();
~ byte[] byteArray = new
byte[Encoding.Unicode.GetByteCount(charArray, 0, charArray.Length)];

~ Encoding.Unicode.GetBytes(charArray, 0, charArray.Length,
byteArray, 0);

// Get string from the byte array
~ string challenge = Encoding.Unicode.GetString(byteArray, 0,
byteArray.Length);
~ Console.WriteLine(challenge);

The challenge is printed just fine (Hello World! plus the PI symbol). So
it really depends on what the initial content of your bytearray is. If
you convert it to ascii, what is the content? Can you print it?

Mark Relly wrote:

| When I say seeing the contents of the string yes I do mean using
| System.WriteLine.
|
| The code below
| challenge = Encoding.Unicode.GetString(byteArray,0,byteArray.L ength);
|
| Console.WriteLine(challenge);
|
| Will give me the output "???????????????????????"
|
| Which is unfortunate as I cannot "see the contents" in a usefull way
|
| Regards
|
| Mark

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oR5TwEwccQ4rWPgRApgmAJ9aRG8H5riwm4ZEjjoiU4gO+1SstA CeL0fw
95ZqTQ5TFnpebIeb90DTUeU=
=uWun
-----END PGP SIGNATURE-----

Nov 15 '05 #5
An example of the initial content of the byte array is

string challenge = "Please press the Sign
ButtonSTARTOFRANDOMTEXT6D8A4B26C98465D32070FAA0A18 077F43FDB1A3B"

This is then converted to
byte[] byteArray = Encoding.ASCII.GetBytes(challenge);

string stringchallenge = Encoding.Unicode.GetString(byteArray);

This is were when written to Console the string is
"????????????????????????"

I want to be able to view the contents of the "???????????" without having
to convert it back to ascii

--
Regards

Mark

Nov 15 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark, the content of the byte array is ascii-encoded, not unicode
encoded. It's no wonder you're getting "?"s :)

Try changing this line:

| byte[] byteArray = Encoding.ASCII.GetBytes(challenge);

to

| byte[] byteArray = Encoding.Unicode.GetBytes(challenge);

Then you'll be able to view it just fine.

Alternatively, you can insert a null between the characters of your
string, i.e.:

string challenge = "P\0l\0e\0a\0s\0e\0..."

It will print just fine :)

Mark Relly wrote:

| An example of the initial content of the byte array is
|
| string challenge = "Please press the Sign
| ButtonSTARTOFRANDOMTEXT6D8A4B26C98465D32070FAA0A18 077F43FDB1A3B"
|
| This is then converted to
| byte[] byteArray = Encoding.ASCII.GetBytes(challenge);
|
| string stringchallenge = Encoding.Unicode.GetString(byteArray);
|
| This is were when written to Console the string is
| "????????????????????????"
|
| I want to be able to view the contents of the "???????????" without
having
| to convert it back to ascii
|
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oUlgwEwccQ4rWPgRAktFAJ0ZxSCciVIOZmDMOrel3RDdItbNdA Cfachm
4NYrS7q/kN+Q0fDG3giQkGE=
=5Thw
-----END PGP SIGNATURE-----

Nov 15 '05 #7
Thanks a lot Ray I'll try that.

Cheers!

Mark

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Um, of course the "inserting nulls between characters" is just to prove
that the byte array is not unicode encoded since you did this:

byteArray = Encoding.ASCII.GetBytes()

:) in general you'd want to just make sure that that byte array's
content is unicode encoded though.
Mark Relly wrote:
| Thanks a lot Ray I'll try that.
|
| Cheers!
|
| Mark
|
| *** Sent via Developersdex http://www.developersdex.com ***
| Don't just participate in USENET...get rewarded for it!
- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/oa5RwEwccQ4rWPgRAu3qAJ0fKChECJivgSNbBqquoxj+ANh6jg CffS1r
9hy+xcJO8iibeMOMCeVBcm0=
=/4oV
-----END PGP SIGNATURE-----

Nov 15 '05 #9

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

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
8
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
6
by: Jeff | last post by:
Hi - I'm setting up a streamreader in a VB.NET app to read a text file and display its contents in a multiline textbox. If I set it up with System.Text.Encoding.Unicode, it reads a unicode...
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.