Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to populate a ByteArray from a String

(0) ShareShare
ReportReport
Posted on by 3,231

Hi

In C# you would use this code to populate a ByteArray from a String:

string postData = "xml content";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);

How to do it in X++?

Thanks, Jan

*This post is locked for comments

  • Suggested answer
    Brandon Wiese Profile Picture
    17,788 on at
    Re: How to populate a ByteArray from a String

    AX4 doesn't support arrays in that fashion.  You can loosely type a variable as a byte array using CLRObject.

    The following code works.

        CLRObject               byteArray;
        System.Text.Encoding    encoding;
        str                     string;
        int                     byte, i, arrayLength;
        ;

        string = "Sample string";
        encoding = System.Text.Encoding::get_UTF8();
        byteArray = encoding.GetBytes(string);

        arrayLength = byteArray.get_Length();
        for (i = 0; i < arrayLength; i++)
        {
            byte = byteArray.GetValue(i);
            info(strfmt("%1", byte));
        }

  • Andri Prawira Profile Picture
    20 on at
    Re: How to populate a ByteArray from a String

    how to use it in ax 4 ? .. i got error when using system.byte[] ...

  • Suggested answer
    David Kidder Profile Picture
    on at
    Re: How to populate a ByteArray from a String

    Marking as suggested answer

  • Suggested answer
    David Kidder Profile Picture
    on at
    Re: How to populate a ByteArray from a String
    System.Byte can be implicitly converted to X++ int.  You can call GetBytes, iterate over the array and convert the items to int:
     
    str                     postData = "xml content";
    System.Byte[]                     byteArray;
    System.Text.Encoding    encodingUTF8;
    int byte, i, arrayLength;
     
    ;
     
    encodingUTF8 = System.Text.Encoding::get_UTF8();
    byteArray = encodingUTF8.GetBytes(postData);
    arrayLength = byteArray.get_Length();
       
    for (i = 0; i < arrayLength; i++)
    {
        byte = byteArray.GetValue(i);
        print byte;
    }
    pause;
  • Jan M. Profile Picture
    3,231 on at
    Re: How to populate a ByteArray from a String

    Of course you can it direct, until you have to go back to AX datatype, than you have to use CLRInterop for conversion. There is the point where not all types can be handeld in AX.

  • Oleg Abapolov Profile Picture
    21 on at
    Re: How to populate a ByteArray from a String

    The very direct approach is to do the same in AX (2009):

        str                     postData = "xml content";
        System.Byte[]           byteArray;
        System.Text.Encoding    encodingUTF8;
        ;
     
        encodingUTF8 = System.Text.Encoding::get_UTF8();
        byteArray = encodingUTF8.GetBytes(postData);

    It depends on what for the array of bytes is required.

     

  • Jan M. Profile Picture
    3,231 on at
    Re: How to populate a ByteArray from a String

    Hi

    After playing around with ClrInterop what did not work we used .NET classes and made a reference in AOT.

    Regards

  • Community Member Profile Picture
    on at
    Re: How to populate a ByteArray from a String

    Hi jan,

    Were u able to solve the above issue. can u guide me on this.

    thnx

    KrPr

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics AX (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 100 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 48

#3
Zain Mehmood Profile Picture

Zain Mehmood 6 Moderator

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans