web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How To Convert From Hex String to UTF8 String

(1) ShareShare
ReportReport
Posted on by 1,036

Greetings dears,

I'm trying to get the string (Normal string human readable) value of a hex string.

I have created a string with a value, then was able to change it to a hex string after converting the string to an array of bytes.

But I'm unable now to go back to the normal string value from Hex.

I'm getting a wrong value at the end.

With the help of this C# post I have been able to write this code:

public static void main(Args _args)
    {
    //-- From UTF-8 String to Hex
        System.Text.Encoding    encodingUTF8;
        encodingUTF8 = System.Text.Encoding::get_UTF8();
        str text = "Hello World";
        str hexString = System.BitConverter::ToString(encodingUTF8.GetBytes(text));

        //-- From Hex to UTF-8 String:
        hexString = strReplace(hexString, "-", "");
        int NumberChars = strLen(hexString);
        System.Byte[] bytes = new System.Byte[NumberChars/2]();
        for (int i = 0; i < NumberChars; i  = 2)
            bytes.SetValue(System.Convert::ToByte(subStr(hexString,i, 2), 16), i/2);
        str fromHexString = encodingUTF8.GetString(bytes);
        //-- End Hex
        info(fromHexString);
    }

The original Text is "Hello World", but the end result is "H�V���v�&�"

 

What am I missing/doing wrong?

Isn't there a built-in method that will convert from Hex to Bytes Array, similar to C#'s System.Convert.FromHexString(String s) ?

Any advice is appreciated :)

Thanks in advance!

I have the same question (0)
  • Suggested answer
    Alex VN Profile Picture
    1,994 on at

    Hi,

    As you already have the function available in C# then you can create a reference to C# library to use the function. You might refer to:

    docs.microsoft.com/.../write-business-logic

    I tried to convert string to hex and vice versa successfully in X++ with the above approach.

    Hope this helps.

    Regards,

    Anh Ong

  • Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    If you want something like System.Convert.FromHexString(), why don't you simply use this method directly, rather than looking for another similiar method?

    Regarding the problem with your code, please debug it and check if you have the same bytes in both arrays. If not, you'll know that the problem is already in the byte array and you don't have to bother with the conversion to string.

  • Momochi Profile Picture
    1,036 on at

    Oh, I have to create a C# class and reference it? This is cool !

    I'm a beginner still learning, will try and reply back once succeeded :)

  • Momochi Profile Picture
    1,036 on at

    Hi Martin!

    I'm sorry what do you mean by directly using it?

    I tried to look for this method through x++, but I didn't find it available.

    I tried "System.Convert::" but the method was not available unfortunately, only base64 methods.

    Will check the byte array.

    Thanks for the help!

  • Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    Yes, you can easily add C# libraries. I do it in this way:

    • Add a C# class library project to the same solution where you have your X++ project. Make sure that types that you want to use from X++ are public.
    • Build the C# project
    • Go the X++ project, right-click References, choose Add reference and add a reference to the C# project. Then you can use your C# classes from X++.
    • Add the DLL with binaries created from your C# project to version control.

    Regarding FromHexString(), it seems that it doesn't exist in .NET Framework; just in .NET Core. :-(

  • Momochi Profile Picture
    1,036 on at

    Thank you Martin!

    This is very helpful and enlightening, will definitely try and reply back! :D

  • Suggested answer
    Chizl.com Profile Picture
    2 on at
    FYI..  Your for loop will keep you in an infinite loop.
    You have :
    for (int i = 0; i < NumberChars; i = 2)
    ...
    
    Should be: 
    for (int i = 0; i < NumberChars; i += 2)
    ...
     
    Though, if you remove the loop all together you can do this instead.
    //-- Setup
    System.Text.Encoding encodingUTF8 = System.Text.Encoding.UTF8;
    System.String text = "Hello World";
    
    //-- From UTF-8 String to Hex String, and remove dashes:
    System.String hexString = System.BitConverter.ToString(encodingUTF8.GetBytes(text)).Replace("-", "");
    
    //-- From Hex String to Byte array:
    System.Byte[] byteArray = System.Linq.Enumerable.Range(0, hexString.Length)
        .Where(x => x % 2 == 0)
        .Select(x => System.Convert.ToByte(hexString.Substring(x, 2), 16))
        .ToArray();
    
    //-- From Byte Array to UTF-8 String:
    System.String fromHexString = encodingUTF8.GetString(byteArray, 0, byteArray.Length);
    
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans