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: 

Convert Hexa to Bin

Former Member
0 Kudos

I want to convert an Hexa to Binary

Exemple : the filed KCUMUL has hexa value

KUMUL = 8000008000000u2026.

This 8000008u2026. Has the Binary equivalent 1000000000000000000000001000u2026.

Is there a function or class ?

Thanks in advance for any hep

Soufiene

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Soufiene,

Sandra is right, just in case you still haven't got it done or haven't cracked it below is a simple piece of code for the same.


DATA: l_data    TYPE kumul,
      l_dat     TYPE string,
      l_bin     TYPE string,
      l_temp    TYPE char4,
      l_hexchar TYPE c,
      l_offset  TYPE i,
      l_len     TYPE i.

SELECT SINGLE kumul
  FROM t512b
  INTO l_data
 WHERE applikation EQ 'C'
   AND verskey EQ 999
  AND molga EQ '02'
  AND lgart EQ '/388'.

IF sy-subrc EQ 0.
  l_dat = l_data.
  l_len = STRLEN( l_dat ).
  WHILE l_len GT 0.
    l_hexchar = l_dat+l_offset(1).
    l_len = l_len - 1.
    l_offset = l_offset + 1.
    CASE l_hexchar.
      WHEN '0'.
        l_temp = '0000'.
      WHEN '1'.
        l_temp = '0001'.
      WHEN '2'.
        l_temp = '0010'.
      WHEN '3'.
        l_temp = '0011'.
      WHEN '4'.
        l_temp = '0100'.
      WHEN '5'.
        l_temp = '0101'.
      WHEN '6'.
        l_temp = '0110'.
      WHEN '7'.
        l_temp = '0111'.
      WHEN '8'.
        l_temp = '1000'.
      WHEN '9'.
        l_temp = '1001'.
      WHEN 'A'.
        l_temp = '1010'.
      WHEN 'B'.
        l_temp = '1011'.
      WHEN 'C'.
        l_temp = '1100'.
      WHEN 'D'.
        l_temp = '1101'.
      WHEN 'E'.
        l_temp = '1110'.
      WHEN 'F'.
        l_temp = '1111'.
      WHEN OTHERS.
    ENDCASE.
    CONCATENATE l_bin l_temp INTO l_bin.
  ENDWHILE.
ENDIF.

WRITE l_bin.

Regards,

Chen

4 REPLIES 4

Former Member
0 Kudos

Try FM "SCMS_XSTRING_TO_BINARY".

Sandra_Rossi
Active Contributor

Hi Soufiane,

That's ten years I have been digging in SAP and I never saw anything that needs it, and never found one too. A simple algorithm will be faster to develop than finding an existing one.

BR

Sandra

Former Member
0 Kudos

Hi Soufiene,

Sandra is right, just in case you still haven't got it done or haven't cracked it below is a simple piece of code for the same.


DATA: l_data    TYPE kumul,
      l_dat     TYPE string,
      l_bin     TYPE string,
      l_temp    TYPE char4,
      l_hexchar TYPE c,
      l_offset  TYPE i,
      l_len     TYPE i.

SELECT SINGLE kumul
  FROM t512b
  INTO l_data
 WHERE applikation EQ 'C'
   AND verskey EQ 999
  AND molga EQ '02'
  AND lgart EQ '/388'.

IF sy-subrc EQ 0.
  l_dat = l_data.
  l_len = STRLEN( l_dat ).
  WHILE l_len GT 0.
    l_hexchar = l_dat+l_offset(1).
    l_len = l_len - 1.
    l_offset = l_offset + 1.
    CASE l_hexchar.
      WHEN '0'.
        l_temp = '0000'.
      WHEN '1'.
        l_temp = '0001'.
      WHEN '2'.
        l_temp = '0010'.
      WHEN '3'.
        l_temp = '0011'.
      WHEN '4'.
        l_temp = '0100'.
      WHEN '5'.
        l_temp = '0101'.
      WHEN '6'.
        l_temp = '0110'.
      WHEN '7'.
        l_temp = '0111'.
      WHEN '8'.
        l_temp = '1000'.
      WHEN '9'.
        l_temp = '1001'.
      WHEN 'A'.
        l_temp = '1010'.
      WHEN 'B'.
        l_temp = '1011'.
      WHEN 'C'.
        l_temp = '1100'.
      WHEN 'D'.
        l_temp = '1101'.
      WHEN 'E'.
        l_temp = '1110'.
      WHEN 'F'.
        l_temp = '1111'.
      WHEN OTHERS.
    ENDCASE.
    CONCATENATE l_bin l_temp INTO l_bin.
  ENDWHILE.
ENDIF.

WRITE l_bin.

Regards,

Chen

Sandra_Rossi
Active Contributor
0 Kudos

ABAP >= 7.40 (+ space between bytes):

DATA(l_bin) = REDUCE string(                          
  LET l_dat = EXACT string( x_or_xstring_variable ) IN
  INIT t = ``                                         
  FOR j = 0 WHILE j < strlen( l_dat )                 
  NEXT t = t && SWITCH #( l_dat+j(1)                  
                WHEN '0' THEN '0000'                  
                WHEN '1' THEN '0001'                  
                WHEN '2' THEN '0010'                  
                WHEN '3' THEN '0011'                  
                WHEN '4' THEN '0100'                  
                WHEN '5' THEN '0101'                  
                WHEN '6' THEN '0110'                  
                WHEN '7' THEN '0111'                  
                WHEN '8' THEN '1000'                  
                WHEN '9' THEN '1001'                  
                WHEN 'A' THEN '1010'                  
                WHEN 'B' THEN '1011'                  
                WHEN 'C' THEN '1100'                  
                WHEN 'D' THEN '1101'                  
                WHEN 'E' THEN '1110'                  
                WHEN 'F' THEN '1111' )                
             && COND #( WHEN j MOD 2 = 1 THEN ` ` ) ).