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

1. User
Join Date
28.11.2017
Posts
15

## 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  Reply With Quote

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  Reply With Quote

3. User
Join Date
28.11.2017
Posts
15

## 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= 16#55 ascci code of 'U'
2 bytearray= 16#49 ascci code of 'I'
3 bytearray and  bytearray= 16#01 index in hexa of my destination array

bytearray  bytearray bytearray  bytearray 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  Reply With Quote

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 := 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  Reply With Quote

5. User
Join Date
28.11.2017
Posts
15

## 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 := 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 !  Reply With Quote

6. User
Join Date
28.11.2017
Posts
15

## 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   Reply With Quote

7. User
Join Date
28.11.2017
Posts
15

## 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   Reply With Quote

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.  Reply With Quote