0

I am using program decoded the text using UTF16 LE (like the example below) so I need to revert back the encoding to characters to be able to use it.

Or another solution will work with me if I can convert from UTF16LE to UTF16BE, then the convert from UTF16BE to Characters, not an issue.

06F006E006A00750065003B00200043006F006D006D006100

OR

450631062D0628062706200043064A06410620002D062706440643061F0643064406200034064A0620002C064A062F061F06280627064A062C

  • 1
    Can you provide that code that generates the input and the desired results? The hex string you're showing could be encoded as `BigEndianUnicode`, but in this case it's garbled. It should be `006F006E006A00750065003B00200043006F006D006D0061` (corresponding to `onjue; Comma`). The final `00` could be the string termination. – Jimi Aug 27 '19 at 12:20
  • I don't have code to convert to UTF16LE, it is converted by third party Program and they confirmed it is a UTF16LE – Wisam Jameel Sep 02 '19 at 14:41
  • You have to provide the **input source** (your code, not the third-party code). Where does this *thing* come from? Is it a byte array? A `Stream` of any kind? Is it a `string` representing Hex values? A decoded content of a base64String? Anything else? Without code, it's really difficult to suggest how to convert *something* when this *something* is undefined, both in origin and type. – Jimi Sep 02 '19 at 14:57
  • My reply was very clear this is a string coming in text file from a third party application, so which code do you want to see if it is the direct output. I added another example too above. – Wisam Jameel Sep 03 '19 at 14:16
  • That second example reads `Hi how are you? Everything is good?` in Arabic (مرحبا كيف حالك؟كل شي جيد؟باي). But it contains a wrong char at the beginning. That's why I asked how are you receiving this output. If you post the code that gets that string, maybe I can fix it to return a correct value. If what you posted is the complete content you received. Maybe you cut it in the wrong place. – Jimi Sep 03 '19 at 14:39
  • hahaha, it seems I am writing in chines if it is a third party app so that means I don't have access to any code only the output and this is it in full. the code is correct as you translate it using an online conversion most probably but that not the solution cause i need to have this conversion using C# derive. – Wisam Jameel Sep 04 '19 at 09:34

1 Answers1

0

I solved the issue by splitting the hex code to 4 characters each in an array then I swap the code to have it as UTF16BE instead of UTF16LE and after that, I converted the code point to characters.

.....

        string PointCode, words;
        int length = 4;
        int strLength = value.Length;
        int strCount = (strLength + length - 1) / length;
        string[] result = new string[strCount];
        char[] charcters = new char[strCount];

        for (int i = 0; i < strCount; ++i)
        {

            result[i] = value.Substring(i * length, Math.Min(length, strLength));
            strLength -= length;


            //Swap the bytes from UTF16 LE to UTF16 BE

            if (result[i].Length == 4)
            {
                string PointCode1 = result[i].Substring(0, 2);
                string PointCode2 = result[i].Substring(2, 2);

                PointCode = PointCode2 + PointCode1;

                charcters[i] = (char)Int16.Parse(PointCode, System.Globalization.NumberStyles.HexNumber);

            }
            else
            {
                PointCode = "00" + result[i];

                charcters[i] = (char)Int16.Parse(PointCode, System.Globalization.NumberStyles.HexNumber);

            }


        }
        words = (string.Join("", charcters));
        return words;