0

Now i have text = " 0e2a0e270e310e2a0e140e350e040e230e310e1a " can convert to " สวัสดีครับ " and i use C# code here

string unicodeString = "0e2a0e270e310e2a0e140e350e040e230e310e1a";
// Create two different encodings.
Encoding utf8 = Encoding.UTF8;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.

char[] asciiChars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];
utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

return asciiString;

is not work

Mickey
  • 943
  • 1
  • 19
  • 41
Witawat
  • 25
  • 9
  • 5
    Define "is not work"; what happens? Also: why do you expect `"0e2a0e270e310e2a0e140e350e040e230e310e1a"` to become anything other than `"0e2a0e270e310e2a0e140e350e040e230e310e1a"` ? – Marc Gravell Oct 30 '13 at 08:28
  • 1
    possible duplicate of [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – CodeCaster Oct 30 '13 at 08:29
  • BTW, where do you see any ASCII? – John Saunders Oct 30 '13 at 08:31
  • this can't convert to " สวัสดีครับ " – Witawat Oct 30 '13 at 08:32
  • this add \u in text can convert ok but i not know how to add \u in string string unicodeString = "\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35\u0e04\u0e23\u0e31\u0e1a"; can convert to " สวัสดีครับ " thankyou – Witawat Oct 30 '13 at 08:36

1 Answers1

3

The input is hex, not unicode - and the encoding is big-endian utf-16:

string hexString = "0e2a0e270e310e2a0e140e350e040e230e310e1a";

// unscramble the hex
byte[] bytes = new byte[hexString.Length / 2];
for(int i = 0; i < bytes.Length; i++)
{
    bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}

// convert to a string via big-endian utf-16
string result = Encoding.BigEndianUnicode.GetString(bytes); // "สวัสดีครับ"
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900