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 Decimal value to its Hex representation

Former Member
0 Kudos

Hi Experts-

I have a need (in ABAP) for being able to convert a decimal value to its hex representation.

I have Document IDs such as '189' and '192' and there is an image file associated with each document called '000000BD' and '000000C0.jpg' respectively. The C0 is the hex representation of the decimal 192 and BD of 189.

Is there a standard function module that I can use for determining this? If not, any quick custom code you can think of?

Thank you,

- Vik.

5 REPLIES 5

Former Member
0 Kudos

Hi,

Try this code.

REPORT ZC1_NUMBER_CONVERSION .

data: v_hexa type x,

v_deci type i.

v_deci = '192'.

v_hexa = v_deci.

write:/ 'Decimal :' , v_deci , ' = Hexa Decimal :' , v_hexa.

I got the following result,

Number Conversion

Decimal : 192 = Hexa Decimal : C0

Hope this helps you.

Cheers

Kathir~

0 Kudos

Hello Kathir,

There are two points that I would like to mention here -

1. In your code snippet, since v_deci is an integer variable, you should not be assigning a string to it. When you say

v_deci = '192'

You sre trying to convert a character type literal into an integer. Here since there's no problem, you will not notice any difference and the conversion goes through.

But the point is that there is no need for this kind of a conversion in this case. You should be using -

v_deci = 192.

2. in your code, if I give a value of say, 600 to v_deci, then the conversion will go for a toss. The hexadecimal equivalent you will get is 58. Which is clearly wrong. In your case, v_hexa is capable of holding an integer equivalent of only up to 256. to make things most generic, you must use..

data: v_hexa(4) type x,
      v_deci    type i.

the length is given as 4, because an integer is assumed to occupy 4 bytes.

Regards,

Anand Mandalika.

Former Member
0 Kudos

Hi,

Hope you have got your problem solved. If you require the same in a function module that will return a value of size 8

as you have shown, i.e. 192 -> 000000C0 then this code will be helpful i believe.

FUNCTION Z_DECIMAL_TO_HEXA.

*"----


""Local interface:

*" IMPORTING

*" VALUE(V_DECIMAL) TYPE I DEFAULT 0

*" EXPORTING

*" VALUE(V_HEXA_VALUE) TYPE CHAR8

*"----


data: v_hexa type x,

v_cnt type i.

v_hexa = v_decimal.

v_hexa_value = v_hexa.

v_cnt = strlen( v_hexa_value ).

v_cnt = 8 - v_cnt.

do v_cnt times.

concatenate '0' v_hexa_value into v_hexa_value.

enddo.

ENDFUNCTION.

Then if you requirement is met please close this topic.

Thanks and Regards,

Kathir~

Former Member
0 Kudos

Hi Vik,

I don't see a need for a Function Module in this case. As Kathir had mentioned, a simple type conversion will do the trick. Push an integer into a Hex field and you'll get what you want.

Also, I notice that quite a few of your recent postings have not been closed. Could you please mark them as answered ?

Regards,

Anand Mandalika.

Former Member
0 Kudos

Hi Poornanand,

First of all thanks for your valid points. Basically, i was trying to help with a logic and did not investigate much say handling large values, thinking that the concerned will take care of the same.

Thanking you again for your suggestions.

Thanks & Regards,

Kathir~