Article

How-To Convert a CHARACTER to HEXADECIMAL and VICE-VERSA

« Go Back

Information

 
Article Number000059004
EnvironmentProduct: Progress
Product: OpenEdge
Version: All Supported Versions
OS: All Supported Platforms
Question/Problem Description
How to convert a character to hex and back
Steps to Reproduce
Clarifying Information
Error Message
Defect/Enhancement Number
Cause
Resolution

Following are 2 programs, One converts a printable character into Hexadecimal value. The other converts any Hexadecimal Value to ASCII value as well as character. Non printable values will show only the ASCII value. These can be modified to suit your needs.
  1. To convert a character to Hexadecimal:
  • Run hex.p from the procedure editor.
  • Enter any character <R>.
           Hexadecimal is displayed in the format "0x55"
  1. To convert Hexadecimal to ASCII & Character:
  • Run convhex.p from the procedure editor.
  • Enter a Hex value in the format "0x55"  <R>
           ASCII and Character are displayed.

/* hex.p */
DEFINE VARIABLE getchar   AS CHARACTER  NO-UNDO 
    FORMAT "x(1)" LABEL "Character".
DEFINE VARIABLE asc-value AS INTEGER    NO-UNDO.
DEFINE VARIABLE c         AS CHARACTER  NO-UNDO.
DEFINE VARIABLE hx        AS CHARACTER  NO-UNDO 
    FORMAT "x(4)" LABEL "Hexadecimal".

DEFINE VARIABLE q         AS INTEGER    NO-UNDO.
DEFINE VARIABLE rem       AS INTEGER    NO-UNDO.
DEFINE VARIABLE r         AS INTEGER    NO-UNDO.

REPEAT:
    UPDATE getchar.
    IF getchar = ? THEN 
      getchar = "?".

    asc-value  = ASC(getchar).

    rem = asc-value MODULO 16.
    q   = ( asc-value - rem ) / 16.

    IF rem GE 10 THEN DO:
      r = rem MODULO 10.
      c = KEYLABEL(KEYCODE("A") + r).
    END.
    ELSE 
      c = STRING(rem).

    hx = "0x" + STRING(q) + c.
    DISPLAY hx.
END.

/* convhex.p */
DEFINE VARIABLE charset   AS CHARACTER  NO-UNDO 
    FORMAT "x(1)" LABEL "Character".
DEFINE VARIABLE str       AS CHARACTER  NO-UNDO.
    
DEFINE VARIABLE asc-value AS INTEGER    NO-UNDO LABEL "ASCII".
DEFINE VARIABLE rem       AS INTEGER    NO-UNDO.
DEFINE VARIABLE r         AS INTEGER    NO-UNDO.
DEFINE VARIABLE hx        AS CHARACTER  NO-UNDO 
    FORMAT "x(4)" LABEL "Hexadecimal".

REPEAT:
    UPDATE hx
    VALIDATE( hx BEGINS "0x", 
              'Must supply Hexadecimal in the 0x format e.g.0x55 ') 
        HELP "Enter a Hexadecimal Value".

    str        = SUBSTRING(hx,3,1).
    asc-value  = INTEGER(str) * 16.

    str = CAPS(SUBSTRING(hx,4,1)).
    IF str GE "A" THEN DO:
      r = KEYCODE(str) - KEYCODE("A").
      rem = 10 + r.
    END.
    ELSE 
      rem = INTEGER(str).

    asc-value  = asc-value + rem.
    charset = CHR(asc-value).
    DISPLAY asc-value charset.
END.
Workaround
Notes
For newer versions of OpenEdge which support the HEX-DECODE function the following code will convert a string of hex values to a character string:

function HexToString returns character (InputData as character):
    return get-string(hex-decode(InputData),1).
end function.

display HexToString("46494E53").
Attachment 
Last Modified Date4/28/2015 6:53 PM


Feedback
 
Did this article resolve your question/issue?

   

Your feedback is appreciated.

Please tell us how we can make this article more useful. Please provide us a way to contact you, should we need clarification on the feedback provided or if you need further assistance.

Characters Remaining: 1025