Feedback
Did this article resolve your question/issue?

   

Article

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

« Go Back

Information

 
TitleHow-To Convert a CHARACTER to HEXADECIMAL and VICE-VERSA
URL NameHow-To-Convert-a-CHARACTER-to-HEXADECIMAL-and-VICE-VERSA
Article Number000174218
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").
Last Modified Date4/28/2015 6:53 PM
Attachment 
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.