Get the unicode of string

Are there any functions in C# such that when I pass a string into the function, it returns the unicode of the string
For example, if I pass the string "abc" into the function, it returns the string "616263" (or "610062006300").

Thanks for answering~




Answer this question

Get the unicode of string

  • .NetRocks

    O, yeah, i got it. It's the .ToString("X2") function.
    thanks, thanks~

  • Luhz

    I just want to call out that there are two ways to cast Unicode to an array of numbers, and this thread started looking for unicode numbers.

    If you go to bytes, you may get more bytes than you had characters and each value would be between 0-255.

    The second way would convert each unicode character to its numeric representation. In that case you would have exactly the same number of numbers in your array as characters in your string, and the potential values would run from 0 up to 65K.

    (If you want the second but go with the first, you won't notice until you start testing with 2- and 3-byte characters).



  • Steven Guo

    Yah, that works
    but actually I'm looking for the code that works in a web application... perhaps I've posted in the wrong forum. Anyway, can any body tell me how to do this
    Many thanks!

  • Karthik Raman

     JoeKYTse wrote:
    Yah, that works
    but actually I'm looking for the code that works in a web application... perhaps I've posted in the wrong forum. Anyway, can any body tell me how to do this
    Many thanks!


    James Kovacs solution can be used in any scenario.  Instead of writing out to the console, just concatenate each of the byte values into a string (calling ToString on the individual bytes first).  You might also want to create a StringBuilder, if you are going to do a number of concatenation operations.

  • Chad Lynch

    This should do it for you:

    byte[] bytes = System.Text.Encoding.Unicode.GetBytes("abc");

    foreach(byte b in bytes) {

    Console.Write("{0:x2}", b);

    }

    Console.WriteLine();


     


  • Windows_Vista

    take a look at the encoding class. this should do what you want; its not a c# feature though, this is simply a part of the bcl.

    WM_HOPETHISHELPS
    thomas woelfer

  • Get the unicode of string