Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to transfer hexadecimal Value to String

former_member189009
Active Participant
0 Kudos

when I debug , I found the variable save as Hexadecimal Value, when there is dump, we will see this Hexadecimal Value in ST22. so I wonder is there any FM or method can transfer Hexadecimal Value to string.

such as:

Hexadecimal Value:   003200300000242000

I want to know the corresponding string.

Thank you so much for your response!

1 ACCEPTED SOLUTION

PeterJonker
Active Contributor

I searched in SE37 (f4) with *hex* and one of the function modules that came up was:

CACS_CONVERT_HEX_TO_STRING

Maybe try it or otherwise:

HR_RU_CONVERT_HEX_TO_STRING

also found with F4 in se37

Edit:

I tried both of them and only HR_RU_CONVERT_HEX_TO_STRING seems to be working correct.

10 REPLIES 10

PeterJonker
Active Contributor

I searched in SE37 (f4) with *hex* and one of the function modules that came up was:

CACS_CONVERT_HEX_TO_STRING

Maybe try it or otherwise:

HR_RU_CONVERT_HEX_TO_STRING

also found with F4 in se37

Edit:

I tried both of them and only HR_RU_CONVERT_HEX_TO_STRING seems to be working correct.

0 Kudos

thank you for your sincere answer!

such  as i want to transfer 0273718683303D   but i couldn't get the right result 273718633.03-

0 Kudos

if I translate your hex value 0273718683303D  online at

http://www.string-functions.com/hex-string.aspx

The result is:

sq†ƒ0=

and  the function module HR_RU_CONVERT_HEX_TO_STRING gives :

#sqЖГ0=

However if I first convert your string 273718633.03- to hexadecimal on the same website, the hex value is:

3237333731383633332e30332d

If I use the function module HR_RU_CONVERT_HEX_TO_STRING and put this value it translates to

273718633.03-

So is your hexadecimal value correct ?

0 Kudos

you are right.. but i just confuse that when i debug the program, the hex value is 0273718683303D just as the image in the last reply.

thank you very much,

dork_nubla
Participant
0 Kudos

Hi Zhang,

You may use  function module HR_KR_XSTRING_TO_STRING to convert it to string.

Regards,

Dexter

former_member184569
Active Contributor
0 Kudos

Try these FMs

NLS_STRING_CONVERT_TO_SYS (parameters : sy-langu, hexstring, 'MS')

HR_PSD_CONV_XSTRING_TO_STRING

HR_RU_CONVERT_HEX_TO_STRING

HR_RU_CONVERT_HEX_TO_CP

AIN_EPC_HEX_TO_CHAR

0 Kudos

thank you for your sincere answer. I am so sorry , maybe I want to transfer Hexadecimal to number

0 Kudos

Hi zhang zongjie

You can try like this

DATA v_hex TYPE X.
DATA v_num TYPE i.

v_hex = '16'.
v_num = V_hex.
write: V_num.

Former Member
0 Kudos

Hi,

You can try the following code to convert the Hexadecimal to String:-

DATA : HEX1  TYPE  XSTRING VALUE '0A01',
            HEX2  TYPE  XSTRING VALUE 'ST22',
    
       DEC1(10) TYPE N,
       DEC2(10) TYPE N.

DEC1 = HEX1.
DEC2 = HEX2.

WRITE:'Hexadecimal value', 50 'Decimal value'.
WRITE:/ HEX1 NO-ZERO, 50 DEC1 NO-ZERO .
WRITE:/ HEX2 NO-ZERO, 50 DEC2 NO-ZERO .

Former Member
0 Kudos

Hi,

You can try and check the below code snippet and use accordingly. Hope it might help.

*&---------------------------------------------------------------------*
*&      Form  convert_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_V_TEXT1  text
*----------------------------------------------------------------------*
FORM convert_text USING    p_text1 TYPE znotes
                  CHANGING p_text2 TYPE znotes.

  DATA : p_input TYPE char2048,"text
         p_output TYPE char2048,
         p_input2 TYPE char2048,
         p_output2 TYPE char2048.

  CLEAR : p_output,
          p_output2,
          p_input,
          p_input2,
          p_text2.

  REFRESH : gt_conversion[].

*************************************************************
  PERFORM: f_set_non_print_values CHANGING gt_conversion.

  p_input = p_text1.

*converting text to hexadecimal value
  PERFORM f_convert_ascii_to_hex USING    gt_conversion
                                               p_input
                                      CHANGING p_output.

  IF NOT p_output IS INITIAL.
*changing 0A to 23 i.e.#
    PERFORM change CHANGING p_output.

  ENDIF.

  IF NOT p_output IS INITIAL.
    CONDENSE p_output NO-GAPS.
    p_input2 = p_output.
*changing hexadecimal to text
    PERFORM f_convert_hex_to_ascii USING    gt_conversion
                                                p_input2
                                   CHANGING p_output2.
  ENDIF.
  CLEAR p_text2.

  CONDENSE p_output2.
  p_text2 = p_output2.

ENDFORM.                    " convert_text

*&---------------------------------------------------------------------*
*&      Form  f_convert_ascii_to_hex
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_CONVERSION  text
*      -->P_P_INPUT  text
*      <--P_P_OUTPUT  text
*----------------------------------------------------------------------*
FORM f_convert_ascii_to_hex USING    rt_conversion TYPE gtt_conversion
                                     rv_str_inp    TYPE char2048
                            CHANGING rv_str_out    TYPE char2048.

  CHECK NOT rv_str_inp IS INITIAL.
  DATA: ls_conversion TYPE gty_conversion,
        lv_strlen     TYPE gty_syfdpos,
        lv_index      TYPE gty_syindex,
        lv_spl_char   TYPE gty_char01,
        lv_ascii_val  TYPE gty_ascii_val,
        lv_hex_val    TYPE gty_ascii_val.
  FIELD-SYMBOLS: <lfs_hex> TYPE x.
  CLEAR: lv_ascii_val, rv_str_out, lv_strlen.
  lv_strlen = strlen( rv_str_inp ).
  DO lv_strlen TIMES.
    CLEAR: lv_index.
    lv_index = sy-index - 1.
    IF rv_str_inp+lv_index(1) EQ '<'.
      MOVE: 'X' TO lv_spl_char.
    ENDIF.
    IF lv_spl_char EQ 'X'.
      CONCATENATE lv_ascii_val rv_str_inp+lv_index(1) INTO lv_ascii_val.
      IF rv_str_inp+lv_index(1) EQ '>'.
        TRANSLATE lv_ascii_val TO UPPER CASE.
        CLEAR: lv_spl_char.
      ELSE.
        CONTINUE.
      ENDIF.
    ELSE.
      MOVE: rv_str_inp+lv_index(1) TO lv_ascii_val.
    ENDIF.
    READ TABLE rt_conversion INTO ls_conversion
         WITH KEY ascii_val = lv_ascii_val.
    IF sy-subrc IS INITIAL.
      MOVE: ls_conversion-hex_val TO lv_hex_val.
    ELSE.
      ASSIGN rv_str_inp+lv_index(1) TO <lfs_hex> TYPE 'X'.
      MOVE: <lfs_hex> TO lv_hex_val.
    ENDIF.
    CONCATENATE rv_str_out lv_hex_val INTO rv_str_out.
    CLEAR: lv_ascii_val, lv_hex_val.
  ENDDO.

ENDFORM.                    " f_convert_ascii_to_hex
*&---------------------------------------------------------------------*
*&      Form  f_convert_hex_to_ascii
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      rt_conversion type gtt_conversion
*      rv_str_inp    type gty_string
*      rv_trans      type gty_char01
*      rv_str_out type gty_string
*
*----------------------------------------------------------------------*
FORM f_convert_hex_to_ascii USING    rt_conversion TYPE gtt_conversion
                                     rv_str_inp    TYPE char2048
                            CHANGING rv_str_out    TYPE char2048.

  CHECK NOT rv_str_inp IS INITIAL.
  DATA: ls_conversion TYPE gty_conversion,
        lv_strlen     TYPE gty_syfdpos,
        lv_index      TYPE gty_syindex,
        lv_hex_val    TYPE gty_hex_val,
        lv_ascii_val  TYPE gty_ascii_val.
  FIELD-SYMBOLS: <lfs_ascii> TYPE c.
  CLEAR: rv_str_out, lv_strlen, lv_index.
  UNASSIGN <lfs_ascii>.
  lv_strlen = strlen( rv_str_inp ).
  DO.
    CLEAR: lv_hex_val, lv_ascii_val.
    READ TABLE rt_conversion INTO ls_conversion
         WITH KEY hex_val = rv_str_inp+lv_index(2).
    IF     sy-subrc IS INITIAL.
      MOVE: ls_conversion-ascii_val TO lv_ascii_val.
    ELSE.
      MOVE: rv_str_inp+lv_index(2)  TO lv_hex_val.
      ASSIGN lv_hex_val TO <lfs_ascii> TYPE 'C'.
      MOVE: <lfs_ascii> TO lv_ascii_val.
****** to manipulate space*******
      IF lv_ascii_val IS INITIAL.
        lv_ascii_val = '|'.
      ENDIF.
*********************************
      UNASSIGN <lfs_ascii>.
    ENDIF.
    CONCATENATE rv_str_out lv_ascii_val INTO rv_str_out.
    lv_index = lv_index + 2.
    IF lv_index GE lv_strlen.
      EXIT.
    ENDIF.
  ENDDO.
*converting '|' to space******
  SEARCH rv_str_out FOR '|'.
  IF sy-subrc IS INITIAL.
    DO.
      REPLACE '|' WITH space INTO rv_str_out.
      SEARCH rv_str_out FOR '|'.
      IF NOT sy-subrc IS INITIAL.
        EXIT.
      ENDIF.
    ENDDO.
  ELSE.
    EXIT.
  ENDIF.
*********************************
ENDFORM.                    " f_convert_hex_to_ascii
*&---------------------------------------------------------------------*
*&      Form  f_set_non_print_values
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--RT_CONVERSION  text
*----------------------------------------------------------------------*
FORM f_set_non_print_values CHANGING rt_conversion TYPE gtt_conversion.
  CLEAR: rt_conversion.
  PERFORM f_set_value
          USING: '00' '<NUL>' CHANGING rt_conversion,
                 '01' '<SOH>' CHANGING rt_conversion,
                 '02' '<STX>' CHANGING rt_conversion,
                 '03' '<ETX>' CHANGING rt_conversion,
                 '04' '<EOT>' CHANGING rt_conversion,
                 '05' '<ENQ>' CHANGING rt_conversion,
                 '06' '<ACK>' CHANGING rt_conversion,
                 '07' '<BEL>' CHANGING rt_conversion,
                 '08' '<BS>'  CHANGING rt_conversion,
                 '09' '<TAB>' CHANGING rt_conversion,
                 '0A' '<LF>'  CHANGING rt_conversion,
                 '0B' '<VT>'  CHANGING rt_conversion,
                 '0C' '<FF>'  CHANGING rt_conversion,
                 '0D' '<CR>'  CHANGING rt_conversion,
                 '0E' '<SO>'  CHANGING rt_conversion,
                 '0F' '<SI>'  CHANGING rt_conversion,
                 '10' '<DLE>' CHANGING rt_conversion,
                 '11' '<DC1>' CHANGING rt_conversion,
                 '12' '<DC2>' CHANGING rt_conversion,
                 '13' '<DC3>' CHANGING rt_conversion,
                 '14' '<DC4>' CHANGING rt_conversion,
                 '15' '<NAK>' CHANGING rt_conversion,
                 '16' '<SYN>' CHANGING rt_conversion,
                 '17' '<ETB>' CHANGING rt_conversion,
                 '18' '<CAN>' CHANGING rt_conversion,
                 '19' '<EM>'  CHANGING rt_conversion,
                 '1A' '<SUB>' CHANGING rt_conversion,
                 '1B' '<ESC>' CHANGING rt_conversion,
                 '1C' '<FS>'  CHANGING rt_conversion,
                 '1D' '<GS>'  CHANGING rt_conversion,
                 '1E' '<RS>'  CHANGING rt_conversion,
                 '1F' '<US>'  CHANGING rt_conversion.
ENDFORM.                    " f_set_non_print_values
*&---------------------------------------------------------------------*
*&      Form  f_set_value
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->LV_HEX     text
*      -->LV_ASCII   text
*      <--RT_CONVERSION  text
*----------------------------------------------------------------------*
FORM f_set_value USING    lv_hex        TYPE gty_hex_val
                          lv_ascii      TYPE gty_ascii_val
                 CHANGING rt_conversion TYPE gtt_conversion.
  DATA: ls_conversion TYPE gty_conversion.
  MOVE: lv_hex   TO ls_conversion-hex_val,
        lv_ascii TO ls_conversion-ascii_val.
  APPEND ls_conversion TO rt_conversion.
ENDFORM.                    " f_set_value
*&---------------------------------------------------------------------*
*&      Form  change
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_P_OUTPUT  text
*----------------------------------------------------------------------*
FORM change CHANGING p_p_output TYPE char2048.

  SEARCH p_p_output FOR '0A'.
  IF sy-subrc IS INITIAL.
    DO.
      REPLACE '0A' WITH '23' INTO p_p_output.
      SEARCH p_p_output FOR '0A'.
      IF NOT sy-subrc IS INITIAL.
        EXIT.
      ENDIF.
    ENDDO.
  ELSE.
    EXIT.
  ENDIF.
ENDFORM.                    " change