I have a string like "sample". I need to get a string of it in hex format With DotNET from C/AL

Michele_GRS18Michele_GRS18 Posts: 5Member
edited 2018-11-05 in NAV Tips & Tricks
Good morning everyone,

I need your help, I thank you in advance

I have a string like "sample". I need to get a string of it in hex format; like this:

%48%65%6C%6C%6F%20%57%6F%72%6C%64%21 <=> 'Hello World!'

I need a metod that let me Conversion from and to hexadecimal format. I'm programming on the Dynamics Nav 2015 platform.


Thank you so much in advance

Michele

Best Answer

  • Michele_GRS18Michele_GRS18 Posts: 5
    Accepted Answer
    Why C/AL is not enough and you need .NET based solution?
    FOR i := 1 TO STRLEN(InText) DO BEGIN
      //c, UpperByte, LowerByte : Integer
      //UpperByteChar, LowerByteChar : Char
      c := InText[i];
      UpperByte := c DIV 16;
      LowerByte := c - (UpperByte * 16);
     
      IF LowerByte < 10 THEN
        LowerByteChar  := 48 +LowerByte
      ELSE 
        LowerByteChar  := 87 +LowerByte;
    
      IF UpperByte < 10 THEN
        UpperByteChar := 48+UpperByte
      ELSE 
        UpperByteChar := 87+UpperByte;
      
      HexText += STRSUBSTNO('%%1%2', UpperByteChar, LowerByteChar);
    END;
    


    Because I prefer to exploit the potential that the dotNet platform offers, simply this. Thank you so much,

    Michele

    Source: https://docs.microsoft.com/it-it/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types

    string input = "Hello World!";
    char[] values = input.ToCharArray();
    foreach (char letter in values)
    {
        // Get the integral value of the character.
        int value = Convert.ToInt32(letter);
        // Convert the decimal value to a hexadecimal value in string form.
        string hexOutput = String.Format("{0:X}", value);
        Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
    }
    /* Output:
       Hexadecimal value of H is 48
        Hexadecimal value of e is 65
        Hexadecimal value of l is 6C
        Hexadecimal value of l is 6C
        Hexadecimal value of o is 6F
        Hexadecimal value of   is 20
        Hexadecimal value of W is 57
        Hexadecimal value of o is 6F
        Hexadecimal value of r is 72
        Hexadecimal value of l is 6C
        Hexadecimal value of d is 64
        Hexadecimal value of ! is 21
     */
    

Answers

  • BlackTigerBlackTiger Posts: 1,207Member
    edited 2018-11-02
    "You can’t just ask customers what they want and then try to give that to them.
    By the time you get it built, they’ll want something new.” Steve Jobs
  • Michele_GRS18Michele_GRS18 Posts: 5Member
    89jvhsbsto81.jpg

    HtmlEncode method of the HttpUtility calss doesn't allow to convert the whole string in hexadecimal format but converts only a part of it :neutral:


    Thank you so much in advance

    Michele
  • Michele_GRS18Michele_GRS18 Posts: 5Member

    Are there any other updates?


    Thank you so much in advance

    Michele
  • Slawek_GuzekSlawek_Guzek Posts: 1,647Member
    edited 2018-11-05
    Why C/AL is not enough and you need .NET based solution?
    FOR i := 1 TO STRLEN(InText) DO BEGIN
      //c, UpperByte, LowerByte : Integer
      //UpperByteChar, LowerByteChar : Char
      c := InText[i];
      UpperByte := c DIV 16;
      LowerByte := c - (UpperByte * 16);
     
      IF LowerByte < 10 THEN
        LowerByteChar  := 48 +LowerByte
      ELSE 
        LowerByteChar  := 87 +LowerByte;
    
      IF UpperByte < 10 THEN
        UpperByteChar := 48+UpperByte
      ELSE 
        UpperByteChar := 87+UpperByte;
      
      HexText += STRSUBSTNO('%%1%2', UpperByteChar, LowerByteChar);
    END;
    
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Michele_GRS18Michele_GRS18 Posts: 5Member
    Why C/AL is not enough and you need .NET based solution?
    FOR i := 1 TO STRLEN(InText) DO BEGIN
      //c, UpperByte, LowerByte : Integer
      //UpperByteChar, LowerByteChar : Char
      c := InText[i];
      UpperByte := c DIV 16;
      LowerByte := c - (UpperByte * 16);
     
      IF LowerByte < 10 THEN
        LowerByteChar  := 48 +LowerByte
      ELSE 
        LowerByteChar  := 87 +LowerByte;
    
      IF UpperByte < 10 THEN
        UpperByteChar := 48+UpperByte
      ELSE 
        UpperByteChar := 87+UpperByte;
      
      HexText += STRSUBSTNO('%%1%2', UpperByteChar, LowerByteChar);
    END;
    

    Because I prefer to exploit the potential that the dotNet platform offers, simply this. Thank you so much,

    Michele
  • Michele_GRS18Michele_GRS18 Posts: 5Member
    Accepted Answer
    Why C/AL is not enough and you need .NET based solution?
    FOR i := 1 TO STRLEN(InText) DO BEGIN
      //c, UpperByte, LowerByte : Integer
      //UpperByteChar, LowerByteChar : Char
      c := InText[i];
      UpperByte := c DIV 16;
      LowerByte := c - (UpperByte * 16);
     
      IF LowerByte < 10 THEN
        LowerByteChar  := 48 +LowerByte
      ELSE 
        LowerByteChar  := 87 +LowerByte;
    
      IF UpperByte < 10 THEN
        UpperByteChar := 48+UpperByte
      ELSE 
        UpperByteChar := 87+UpperByte;
      
      HexText += STRSUBSTNO('%%1%2', UpperByteChar, LowerByteChar);
    END;
    


    Because I prefer to exploit the potential that the dotNet platform offers, simply this. Thank you so much,

    Michele

    Source: https://docs.microsoft.com/it-it/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types

    string input = "Hello World!";
    char[] values = input.ToCharArray();
    foreach (char letter in values)
    {
        // Get the integral value of the character.
        int value = Convert.ToInt32(letter);
        // Convert the decimal value to a hexadecimal value in string form.
        string hexOutput = String.Format("{0:X}", value);
        Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
    }
    /* Output:
       Hexadecimal value of H is 48
        Hexadecimal value of e is 65
        Hexadecimal value of l is 6C
        Hexadecimal value of l is 6C
        Hexadecimal value of o is 6F
        Hexadecimal value of   is 20
        Hexadecimal value of W is 57
        Hexadecimal value of o is 6F
        Hexadecimal value of r is 72
        Hexadecimal value of l is 6C
        Hexadecimal value of d is 64
        Hexadecimal value of ! is 21
     */
    
Sign In or Register to comment.