Former Member

# Convert Decimal value to its Hex representation

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.

10|10000 characters needed characters exceeded

### Related questions

• Former Member
Posted on May 02, 2005 at 03:16 PM

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~

10|10000 characters needed characters exceeded
• Former Member

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
Posted on May 03, 2005 at 06:38 AM

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~

10|10000 characters needed characters exceeded
• Former Member
Posted on May 03, 2005 at 06:59 AM

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.

10|10000 characters needed characters exceeded
• Former Member
Posted on May 03, 2005 at 08:21 AM

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~