By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

Unicode In C#?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
-----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

P: n/a
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

P: n/a
-----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

P: n/a
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

P: n/a
-----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

P: n/a
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

P: n/a
-----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 discussion thread is closed

Replies have been disabled for this discussion.