06-13-2011 6:54 PM
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
06-14-2011 8:22 AM
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
06-13-2011 7:09 PM
06-13-2011 10:43 PM
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
06-14-2011 8:22 AM
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
07-05-2023 6:04 PM
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 ` ` ) ).