Unicode In C#?

  • Thread starter Thread starter Mark Relly
  • Start date Start date
M

Mark Relly

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
 
Mark Relly said:
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
 
-----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-----
 
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.Length);

Console.WriteLine(challenge);

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

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

Regards

Mark
 
-----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.Length);
|
| 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+1SstACeL0fw
95ZqTQ5TFnpebIeb90DTUeU=
=uWun
-----END PGP SIGNATURE-----
 
An example of the initial content of the byte array is

string challenge = "Please press the Sign
ButtonSTARTOFRANDOMTEXT6D8A4B26C98465D32070FAA0A18077F43FDB1A3B"

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
 
-----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
| ButtonSTARTOFRANDOMTEXT6D8A4B26C98465D32070FAA0A18077F43FDB1A3B"
|
| 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/oUlgwEwccQ4rWPgRAktFAJ0ZxSCciVIOZmDMOrel3RDdItbNdACfachm
4NYrS7q/kN+Q0fDG3giQkGE=
=5Thw
-----END PGP SIGNATURE-----
 
-----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
|
|
| 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+ANh6jgCffS1r
9hy+xcJO8iibeMOMCeVBcm0=
=/4oV
-----END PGP SIGNATURE-----
 
Back
Top