# Thread: how to convert the content of a variable in decimal into an hexadecimal value

1. User
Join Date
28.11.2017
Posts
16

## how to convert the content of a variable in decimal into an hexadecimal value

hi

i would like to know how to to convert the content of a variable in decimal into an hexadecimal value

2. Employee
Join Date
08.12.2015
Posts
39

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Hello,

The value of a variable is always the same, in decimal, binary, hexadecimal... But you can view the hexadecimal form of a value, in debug (so while you are online or in simulation) doing a right-click on it > Hexadecimal display.
If you want to recover the hexadecimal value of a variable in a STRING you can use the "HTOA" block.

I hope it helps,
Anthony

3. User
Join Date
28.11.2017
Posts
16

## Re: how to convert the content of a variable in decimal into an hexadecimal value

hi anthony

i try to format a value from a DINT into an 8 bytes array

1 bytearray[0]= 16#55 ascci code of 'U'
2 bytearray[1]= 16#49 ascci code of 'I'
3 bytearray[2] and  bytearray[3]= 16#01 index in hexa of my destination array

bytearray[4]  bytearray[5] bytearray[6]  bytearray[7] value in hexa of my parameter

so i am doing a serialize out to fill my value array but the user input the value in decimal i want to write it in the array in hexa

4. Employee
Join Date
08.12.2015
Posts
39

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Hi !

Mmmh... You mean, for example, the user is writing "85" (decimal value of 16#55) but you do not want to write "85" in your array of BYTEs, you want to have "55" (because it's the hexadecimal value), right?

Then I think you can use the HTOA block this way:

MyHexValue_0 := HTOA(DecimalUserValue_0); // Converts the decimal value (85) in hexa and stores it in a STRING ('55')
bytearray[0] := ANY_TO_USINT(MyHexValue_0); // Converts the STRING in an USINT (55) so you can store it in the array
Last edited by anthony.burille : 29th May 2018 at 14:47

5. User
Join Date
28.11.2017
Posts
16

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Originally Posted by anthony.burille
Hi !

Mmmh... You mean, for example, the user is writing "85" (decimal value of 16#55) but you do not want to write "85" in your array of BYTEs, you want to have "55" (because it's the hexadecimal value), right?

Then I think you can use the HTOA block this way:

MyHexValue_0 := HTOA(DecimalUserValue_0);     // Converts the decimal value (85) in hexa and stores it in a STRING ('55')
bytearray[0] := ANY_TO_USINT(MyHexValue_0); // Converts the STRING in an USINT (55) so you can stores it in the array

thanks that is what i want to do !

6. User
Join Date
28.11.2017
Posts
16

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Originally Posted by mos89

thanks that is what i want to do !

Hi have an issue with negative number

7. User
Join Date
28.11.2017
Posts
16

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Originally Posted by mos89

Hi have an issue with negative number
not only negative numbers:
myVarinHex_0 is a tmp variable in DINT

8. Employee
Join Date
08.12.2015
Posts
39

## Re: how to convert the content of a variable in decimal into an hexadecimal value

Hi,

Aaaah... Yes, of course, I was focused on the '55' example, this was easy because converting a STRING to a DINT, it will not recognize this is an haxadecimal form, it processes the value by elimination, from the left to the right:

STRING | DINT
'55'       | 55
'5 6'      | 5
'5E'       | 5
'16#5E' | 16

So "strictly" converting a STRING which contains other characters than numbers, into a DINT, this is not possible.
And of course hexadecimal values are often containing 'A', 'B', 'C', 'D', 'E' and 'F'...
Then, this is not possible to do it using the SERIALIZEIN/SERIALIZEOUT functions because these are using array of bytes.

But... Why storing hexadecimal values?
As I said in the first post, if you store 85 in decimal, or 16#55 or 2#01010101, the value will be the same, it still is 85.
If you strictly want to store the hexadecimal form of a value, then you have to store it in an array of STRING

#### Posting Rules

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•