Search This Blog

Thursday, July 03, 2008

Convert Hexadecimal to Binary to Decimal, Bin to Hex, Hex to Dec

*--- hex to binary
data: b(1) type n,
l_hex type xstring,
l_bin type string.

DO.
GET BIT sy-index OF l_hex INTO b.
IF sy-subrc <> 0. EXIT. ENDIF.
CONCATENATE l_bin b INTO l_bin.
CONDENSE l_bin NO-GAPS.
ENDDO.
write: / l_bin.

*--- Binary to Dec
DATA : dig_c(1) TYPE c,
dig_i TYPE i,
s_bin TYPE string,
l TYPE i,
l_exp TYPE p,
l_dec TYPE p,
c TYPE i.

clear: dig_c, dig_i, s_bin, l, l_exp, l_dec, c.

s_bin = l_bin.
l = strlen( s_bin ).
c = l - 1.
do l times.
dig_c = s_bin.
dig_i = dig_c.
l_exp = 2 ** c .
l_dec = l_dec + dig_i * l_exp.
shift s_bin.
c = c - 1.
enddo.
write: / l_dec.

*--- Bin to Hex test
DATA hex(3) TYPE x.
SET BIT: 09 OF hex TO 1,
10 OF hex TO 0,
11 OF hex TO 1,
12 OF hex TO 1,
13 OF hex TO 0,
14 OF hex TO 1,
15 OF hex TO 0,
16 OF hex TO 1.
WRITE hex.

*--- Hex to Dec test
DATA: l_hex TYPE xstring.
DATA l_int TYPE i.
l_hex = '123BCA'.
l_int = l_hex.
WRITE: / 'Hex:', l_hex.
WRITE: / 'Dec:', l_int.

Additional info:
http://help.sap.com/saphelp_nw04/helpdata/en/b6/e7d716f46711d195200000e8353423/frameset.htm

No comments: