Groups | Blog | Home
all groups > c# > december 2004 >

c# : Unicode convert


[Yosi]
12/7/2004 11:27:07 PM
Hi,
I have a string array includes unicode data, how can I print the char (real
string),
for example:
"\x08\x03\x34\0.\0\x39\0"
What should I do, I want to see the char of this array of unicode.
I want to make convert between Unicode to string and string to Unicode.
[Yosi]
12/8/2004 12:05:03 AM
I want to see "4.1", I successfuly do that:
Byte[] buffer = new Byte[stR.Length];
for(int j=0;j<stR.Length;j++)
{
buffer[j]=Convert.ToByte(stR[j]);
}
string clientcommand = System.Text.Encoding.Unicode.GetString(buffer);

****but I cant do the other way :
string str = "String to convert"
string unicodestring = str.Convert to Unicode ??????



[quoted text, click to view]
[Yosi]
12/8/2004 4:35:10 AM
I want to make an application includes one Text box, and 2 button , user can
write:
"\x2E\x03N\0a\0t\0i\0o\0n\0a\0l\0 \0S\0e\0m\0i\0" then click on convert to
ascii ->"National semi"

or write "National semi"
then click on convert to unicode ->
"\x2E\x03N\0a\0t\0i\0o\0n\0a\0l\0 \0S\0e\0m\0i\0"

I need this to use the result in the USB descriptor definistion

[quoted text, click to view]
Jon Skeet [C# MVP]
12/8/2004 7:56:00 AM
[quoted text, click to view]

Strings are in Unicode already. If you mean you want to see:

8
3
52
0
etc

then just do:

foreach (char c in theString)
{
Console.WriteLine ((int)c);
}

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
12/8/2004 8:48:32 AM
[quoted text, click to view]

Why would you get 4.1 from "\x08\x03\x34\0.\0\x39\0"?

The above is:

Backspace
ETX (not even sure what that is)
4
NUL
..
NUL
9
NUL

I think it would help if you started right from the beginning, and told
us what your original data is, where you get it from, and what you want
to do with it.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
12/8/2004 5:39:56 PM
[quoted text, click to view]

Why would that convert to National semi though? What's the \x2E\x03
doing there to start with?

[quoted text, click to view]

I think the trouble is that you're sort of treating characters as if
they were bytes - you need to be very clear about the difference.
You're effectively using Encoding.Unicode.GetBytes and then treating
each of those bytes as a character, which is likely to lead to problems
later on. Is there any reason for it not to end up as a binary block
which doesn't necessarily need to be cut/paste?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button