5 Replies Latest reply: Apr 23, 2005 8:46 AM by Durairaj Athavan Raja RSS

HEX TO CHAR CONVERSION

Howard bateman
Currently Being Moderated

Gentlemen,

  I had a prior post in which I converted a string to HEX (in order to pass it as a literal to my subroutine [where one of the characters wasn't recognized within the literal]).

NOW I need to reconvert the HEX string back to regular characters. I've tried several variations of my code and checked all the help documentation. Please give me some advice.   FOR NOW, I JUST WANT TO RUN THE RECONVERSION IN A TEST PROGRAM AND THEN TRANSFER THE WORKING CODE TO THE SUBROUTINE.

My code is below:  (arc_buffer-segment contains  a segment returned by my FM)

DATA: HEXSTRINGER(10000).
DATA: STR1(1) VALUE 'C'.
DATA: STR2(2) VALUE '7C'.
DATA: LEN TYPE I VALUE 1.
DATA: STRINGH(10000) TYPE C.
FIELD-SYMBOLS: <fsHex>, <H>.
assign ARC_BUFFER-segment to <fsHEX> type 'X'.
           write <fsHEX> to HEXSTRINGER.

           REPLACE STR1 WITH STR2 INTO HEXSTRINGER length LEN.
            if sy-subrc eq '0'.
               write:/ 'replaced'.
            endif.
* CONVERTS TO HEX
* ATTEMPTS TO CONVERT HEX BACK TO NORMAL CHAR STRING
ASSIGN HEXSTRINGER to <H> TYPE 'C'.
          write <h> to stringh.

 

  • Re: HEX TO CHAR CONVERSION
    Prabhu Peram
    Currently Being Moderated

    Hi bate,

     

      try this exp if anyinfo plz buzz me .

     

    data:

          _c type c,

          _i type i,

          _l type i,

           a type I,

          _o type i,

          t1_ type c,

           a1 type c.

     

    field-symbols : <n> type x.

    field-symbols : <pn> type c.

    field-symbols : <dn1> type x.

    data : rn type i.

    data : rn1(4) type c.

    data : n type c.

     

    describe field P_I_UPLOAD type _c.

    if _c eq 'u'.

    l = strlen( PI_UPLOAD ).

    _i = _l div 2.

     

    do _l times.

    a = sy-index - 1.

    a1 = P_I_UPLOAD+a(1).

    assign a1 to <n> casting.

    move <n> to rn.

    move <pn> to rn1.

    *if rn1+0(1) EQ '#'.

    n = rn1+3(1).

    A1 = n.

    P_I_UPLOAD+a(1) = A1.

    enddo.

    endif.

     

     

     

    regards

    Prabhu

    nethranp@hotmail.com

    • Re: HEX TO CHAR CONVERSION
      Howard bateman
      Currently Being Moderated

      I'm trying to debug this code.

      How do I declare P_I_UPLOAD as to get it described as type 'u' so I can continue processing.

       

      In F1 it looks like type u is a structure without an internal table.

      So would that look something like:

      DATA: P_I_UPLOAD(100) WITH HEADER LINE.

      ?

      • Re: HEX TO CHAR CONVERSION
        Durairaj Athavan Raja
        Currently Being Moderated

        Which version of SAP you are in. The FM was from WAS 6.2 system.

         

        Let me paste the source from the FM here may be you could use.

         

        function stpu1_hex_to_char.
        *"----------------------------------------------------------------------
        *"*"Lokale Schnittstelle:
        *"  IMPORTING
        *"     VALUE(HEX_STRING) TYPE  C
        *"  EXPORTING
        *"     VALUE(CHAR_STRING) TYPE  C
        *"----------------------------------------------------------------------
        constants: begin of control_tags,
              instance_ref(3) type c value '<!>',
              hex_tag(3)      type c value 'X',
              hex_tag_low(3)  type c value 'x',
            end of control_tags.
        
          data: l_char          type c,
                dummy1          type castp_step_name,
                dummy2          type castp_step_name,
                xseq            type xstring,
                conv            type ref to cl_abap_conv_in_ce.
        
          move hex_string to char_string.
        
          while char_string cs control_tags-hex_tag.
            split char_string at control_tags-hex_tag into dummy1 dummy2.
            if dummy2 is initial.
              split char_string at control_tags-hex_tag_low into dummy1 dummy2.
            endif.
            check not dummy2 is initial.
            move dummy2+0(2) to xseq. "HEX_DATA-HEX_CHAR.
            shift dummy2 by 2 places.
            try.
            call method cl_abap_conv_in_ce=>create
              exporting
                encoding                      = 'UTF-8'
        *        ENDIAN                        =
        *        REPLACEMENT                   = '#'
        *        IGNORE_CERR                   = ABAP_FALSE
                input                         = xseq
              receiving
                conv                          = conv
                .
             catch cx_parameter_invalid_range .
             catch cx_sy_codepage_converter_init .
            endtry.
        
            try.
            call method conv->read
              exporting
                n                             = 1
        *        VIEW                          =
              importing
                data                          = l_char
        *        LEN                           =
                .
             catch cx_sy_conversion_codepage .
             catch cx_sy_codepage_converter_init .
             catch cx_parameter_invalid_type .
             catch cx_parameter_invalid_range .
            endtry.
        
            concatenate dummy1 l_char dummy2 into char_string.
          endwhile.
        
        endfunction.

         

        Regards

        Raja

  • Re: HEX TO CHAR CONVERSION
    Prabhu Peram
    Currently Being Moderated

    I wanna tell u one more thing, when u converting hex to Char it display like this

     

    Enc: char to hex

    exp

    a --> 65

     

    Dec :hex to char

    65--> ###a if it is Unixserver

    65--> a### if it is NTserver 

     

    So by using offset length Change the Logic and if u want any help mail to me .....

     

     

    regards

    prabhu

Actions