Application Development and Automation 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: 
Read only

Convert Binary number to Hex

Former Member
0 Likes
5,162

Hi

How to convert Binary number to hex.

Ex : Binary number = 1000

       Hexa Number = 8

Example 2 :

Binary number = 10001010

       Hexa Number = 8A

Please help me to solve this issue.

Moderator message: Sorry no spoon-feeding. Points awarded removed.

Message was edited by: Suhas Saha

9 REPLIES 9
Read only

Former Member
0 Likes
3,812

Hello Nagesh,

try these..

Decimal to binary.


Parameter: Decimal  type p decimals 2.

data: int type i,

      char type c,

      result(16) type c.

WHILE Decimal GE 1.

    INT = Decimal MOD 2.

    Decimal = Decimal DIV 2.

    CHAR = INT.

    CONCATENATE  CHAR RESULT  INTO RESULT .

  ENDWHILE.                            "WHILE Decimal GE 1 .

  WRITE 😕 'BINARY = :',RESULT.

Same Logic you can use for Hexa decimal also.


    INT = Decimal MOD 16.

    Decimal = Decimal DIV 16.

Case int

When 10.

char = 'A'.

When 11

char = 'B'..

...

Rest of code you can do by yourself.

Hexa to deci....

DATA deci TYPE i.

DATA hexa TYPE X.

hexa = '10'.

deci = hexa.

" CRM_EI_KB_CONV_DEC_TO_HEX "

This fm converts any decimal to hexadecimal,

For more info. Please Search on Google how to convert Decimal to binary and Hexa and then try to modify my code

Read only

0 Likes
3,809

HI Chandra Shekhar , I want to convert Binary number to Hexa number , Your code is not helping me.

Please have a look on the screen snap.

Read only

0 Likes
3,809

Hi,

What is the type of the variable where you have the Binary Number in. Is it Character field then you can use OFFSET and CASE statement and get to the HEX equivalent. Or else You can use the SET BIT and GET BIT key words to get the desired results. Check the link below

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

Cheers,

Arindam

Read only

0 Likes
3,809

Arindam

Can you provide me with an example by considering the above chart plz.

Read only

3,809

Enjoy

DATA lv_input TYPE string VALUE '101011'.
DATA: lv_digit TYPE i,
       lv_remain TYPE i,
       lv_loop TYPE i VALUE 0,
       lv_length TYPE i,
       lv_char TYPE string,
       lv_output TYPE i,
       lv_result TYPE string.

lv_length = strlen( lv_input ).

WHILE lv_length > lv_loop.
   lv_digit = lv_input MOD 10.
   lv_input = lv_input / 10.
   lv_output = lv_digit * ( 2 ** lv_loop ).
   lv_remain lv_remain + lv_output .
   lv_loop = lv_loop + 1.
ENDWHILE.
CLEAR: lv_loop , lv_output , lv_input, lv_digit.

WHILE lv_remain > 0.
   lv_digit = lv_remain MOD 16.
   IF lv_remain GE 16.
     lv_remain = lv_remain DIV 16.
   ELSE.
     CLEAR lv_remain.
   ENDIF.
   CASE lv_digit.
     WHEN 10.
       lv_char = 'A'.
     WHEN 11.
       lv_char = 'B'.
     WHEN 12.
       lv_char = 'C'.
     WHEN 13.
       lv_char = 'D'.
     WHEN 14.
       lv_char = 'E'.
     WHEN 15.
       lv_char = 'F'.
     WHEN  OTHERS.
       lv_char = lv_digit.
   ENDCASE.

   CONCATENATE lv_char lv_result  INTO  lv_result.
   CONDENSE  lv_result NO-GAPS.
ENDWHILE.
WRITE: 'Result in Hexa : ', lv_result.

Read only

0 Likes
3,809

Hi Chandra

Thnx for your reply.It is working correctly.

I appreciate for your quick help.

Read only

0 Likes
3,809

Frankly Speaking, I was not in mood to give you exact code. I want you to create by own using my old comment concept, it will clear your  concepts .. But i saw you want it badly. So i shared ..

Good day

Read only

0 Likes
3,368

You Save My Day. Thanks !

Read only

anubhab
Active Participant
0 Likes
3,809

Hello Nagesh,

Please check the below link once, this might help you to solve your problem.

http://saptib.blogspot.in/2008/07/translate-hexadecimal-to-binary-to.html

Regards,

Anubhab