ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Convert ASCII HEX to ASCII Char

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert ASCII HEX to ASCII Char

    Hello, does anyone know how to convert ASCII HEX to ASCII Char?

    I tired using QDCXLATE with a parm of QASCII but I believe the API is assuming that the data is already in EBCDIC format.

    I am trying to translate HEX ASCII data that is being sent from a vendor. The data is encrypted using AES and is sent to me in HEX format. I need to convert it to ASCII character so that I can decrypt it, and then convert it to EBCDIC.

    Thanks

  • #2
    Re: Convert ASCII HEX to ASCII Char

    Hello

    Let's say you have hex 'C1'; that would be an 'A' in ASCII.

    First you have to convert the hex to binary. That is easy enough, because there are only 16 four bit combinations:

    0000 = 0
    0001 = 1
    0010 = 2
    0011 = 3
    0100 = 4
    0101 = 5
    0110 = 6
    0111 = 7
    1000 = 8
    1001 = 9
    1010 = A
    1011 = B
    1100 = C
    1101 = D
    1110 = E
    1111 = F

    You can use compile time arrays for that. Once you have the hex converted into a character representation of a binary value, in this case hex 'C1' = binary '11000001' you can build your ASCII character byte one bit at a time.

    Create an array called bit and use a data structure or pointer to intialise it to x'8040201008040201', so that bit(1) is binary '10000000', bit(2) is '01000000' and so on. Every element of the bit array has only one '1' and its position corresponds to its index.

    In the old days we used to use SETON for this. With free-format you can use the bitwise or built-in-funtion %BITOR:
    Code:
    ASCIIchar = x'00';
    for aX = 1 to 8;
      if %subst(binaryString:aX:1) = '1';
        ASCIIchar = %BITOR(ASCIIchar:bit(aX));
      endif;
    endfor;
    I know it is a little confusing, but if you puzzle it out it makes perfect sense.

    Hope this helps
    Last edited by Flodge; March 20, 2009, 03:15 AM.

    Comment


    • #3
      Re: Convert ASCII HEX to ASCII Char

      You could read this article: Hex to character conversion

      Summary:

      How can I convert between character and hex in RPGIV?

      Use the cvthc and cvtch functions.

      Here are the prototypes.

      The "1" and "2" refer to the number of bytes involved in each conversion.

      For example, to convert from '1F3D' to X'1F3D',
      you want to convert each 2 bytes to 1 byte. So use cvthex2To1.

      Code:
      H bnddir('QC2LE')
      
      D cvthex1To2      PR                  extproc('cvthc')
      D  longReceiver                   *   value
      D  shortSource                    *   value
      D  receiverBytes                10i 0 value
      
      D cvthex2To1      PR                  extproc('cvtch')
      D  shortReceiver                  *   value
      D  longSource                     *   value
      D  sourceBytes                  10i 0 value
      
      Here is some sample code using these functions:
      
      D charval         S              4a   inz('1F3D')
      D hexval          S              5a   inz(X'C1C2C3C4C5')
      
      D charResult      S             10a
      D intResult       S              5i 0
      
       * convert from a character value '1F3D' to an integer
       * value x'1F3D'
      C                   callp     cvthex2To1 (%addr(intResult)
      C                                       : %addr(charval)
      C                                       : %size(intResult) * 2)
      
       * convert from a hex value X'C1C2C3C4C5' to an character
       * value 'C1C2C3C4C5' (ABCDE)
      C                   callp     cvthex1To2 (%addr(charResult)
      C                                       : %addr(hexval)
      C                                       : %size(charResult))

      Comment

      Working...
      X