Unicode convert

G

Guest

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.
 
J

Jon Skeet [C# MVP]

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.

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);
}
 
G

Guest

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 ??????
 
J

Jon Skeet [C# MVP]

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 ??????

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.
 
G

Guest

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

Jon Skeet said:
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 ??????

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.
 
J

Jon Skeet [C# MVP]

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"

Why would that convert to National semi though? What's the \x2E\x03
doing there to start with?
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

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top