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 :
Microsoft Dynamics AX (Archived)

How to populate a ByteArray from a String

(0) ShareShare
ReportReport
Posted on by 3,262

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

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi jan,

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

    thnx

    KrPr

  • Jan M. Profile Picture
    3,262 on at

    Hi

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

    Regards

  • Oleg Abapolov Profile Picture
    39 on at

    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,262 on at

    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.

  • Suggested answer
    David Kidder Profile Picture
    on at
    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;
  • Suggested answer
    David Kidder Profile Picture
    on at

    Marking as suggested answer

  • Andri Prawira Profile Picture
    20 on at

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

  • Suggested answer
    Brandon Wiese Profile Picture
    17,788 on at

    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));
        }

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans