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

6
  • 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, 2019 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 Sep 2, 2019 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 2, 2019 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. Sep 3, 2019 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 3, 2019 at 14:39

1 Answer 1

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.