## Converting Hexidecimal to Integer & Interger to Hexidecimal

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

medelec35
Valued Contributor
Posts: 8619
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2480 times
Been thanked: 3567 times
Contact:

### Converting Hexidecimal to Integer & Interger to Hexidecimal

I have use my own methods for both conversions.
Flowchart will convert in the range of 0 to 32767 (0 to 7FFF)
Flowchart that converts int to hex (from 0 to 32767) without preceding with 0x, and hex letters are upper case.
I could probably get same result using hex2int, but as stated above, use my own method.
Conversions1.jpg (170.86 KiB) Viewed 5639 times

Martin
Attachments
Int2Hex1.fcf
Hex2Int3.fcf
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

medelec35
Valued Contributor
Posts: 8619
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2480 times
Been thanked: 3567 times
Contact:

### Re: Converting Hexidecimal to Integer & Interger to Hexideci

above is created with Flowcode V4 for PIC.

Attached are Flowcode V3 for PIC Versions.

Martin

Edit: I have altered files so the conversion is done within the conversion macro.
That way you just load your game flowchart, and conversion flowcharts.
Export conversion macros.
Then after you import exported macros to your game flowchart, all the necessary variables for conversion will automatic be added.

I am working on just 1 flowchart that converts both ways, but share common variables which keeps memory usage down.
The only thing with that method is some of the common variable names don't mean much.
Will post the all in one as soon as completed it.
Attachments
Int2Hex FC3.fcf
Hex2Int FC3.fcf
These users thanked the author medelec35 for the post:
Spanish_dude (Sun May 08, 2011 10:11 am)
Rating: 5%

Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

### Re: Converting Hexidecimal to Integer & Interger to Hexideci

Hi Martin, I just tried your Hex2Int Flowcode file and it does convert "8000" to -32768, and "FFFF" to -1.
Works perfect !

medelec35
Valued Contributor
Posts: 8619
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2480 times
Been thanked: 3567 times
Contact:

### Re: Converting Hexidecimal to Integer & Interger to Hexideci

Spanish_dude wrote: I just tried your Hex2Int Flowcode file and it does convert "8000" to -32768, and "FFFF" to -1.
Works perfect !

Glad it works for you. I was just basing on the fact that FFFF = 65535
Unless use long in C you will only get -1 since FC is using 16bit signed.

Thanks for letting me know.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

medelec35
Valued Contributor
Posts: 8619
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2480 times
Been thanked: 3567 times
Contact:

### Re: Converting Hexadecimal to Integer & Interger to Hexadeci

Here is the latest and best version! (until the next one )
It is still created with Flowcode V3, so it will work with both Flowcode V3 and V4 versions
This version can convert both ways, using common variables to keep ROM usage down.
Both conversions are done within their own macros.
so if macros are imported to existing flowcharts, all necessary variable names will automatically be imported with it.
One application I can think of is if receiving HEX via USB/RS232 etc.
You will be able to convert to Integer, then use compare icons to operate relays, LED's etc..
Shows all vaiables used for dual conversion
Int2Hex2Int V1.jpg (104.09 KiB) Viewed 5614 times

Here is how conversion works:
From Integer to Hex e.g 1234:

Integer Value is converted to a string using

Code: Select all

ToString\$
Built in function.

Int_Var uses MOD 16 to store remainder of 1234/16 = 2
(1234/16=77 r 2)
Int_Conversion stores 1234
The remainder is the least significant hex value.
="2"
Hex_Val1="2"

Next the 77 is divided by 16:
77/16 = 4 r 13
Using decision branches 13 = "D"
Hex_Val1="2D"

Next the 4 is divided by 16:
4/16 = 0 r 4
="4"
Hex_Val1="2D4"

This process stops as soon as whole number after division = 0 which is now!

Finally a string reversing routine changes "2D4" to "4D2" = Hex_Val2

Conversion of Hexadecimal to Integer:
This is a smaller routine.
Each value from Least significant to most significant is used one at a time.
Taking hex value of 4D2 for example.
When you use a string with a character within square brackets within a calculation box, then the ASCII value of the character is assigned to the string variable.
Using calculation

Code: Select all

Int_Var = Hex_Val1[Index]

Index is x position of each Char. E.g the index for 4 = 0, index for D = 1 and index for 2 = 2

We are working backwards so using Hex value of 2 first.
ASCII of 2 = 50
There is a discussion branch which states if this ASCII value < 64 then

Code: Select all

Int_Var = Int_Var - 48

This is because a number from 0 to 9(ASCII 48 to 57) is used. So 48 must be subtracted from the ASCII value.
50 - 48 = 2

ASCII of D = 68
if this ASCII value >64 then

Code: Select all

Int_Var = Int_Var - 55

This is because an upper case letter is used (A to F = ASCII 65 to 70) so 55 must be subtracted from the ASCII value.
68 - 55 = 13

ASCII of 4 = 52
Again using decision if this ASCII value < 64
52 - 48 = 4

Using the formula: Int value = 2*(16^0) + 13*(16^1) + 4*(16^2)
= 2 + 208 + 1024
= 1234

This is easy to achieve though bit shifting i.e <<1 = bit shift all by left by 1 place this is the same as multiplying by 2^1 = 2. <<2 = 2^2 = 4. <<3 = 2^3 = 8 ect.
Applying this in Flowcode I used:

Code: Select all

Int_Value = Int_Var << Count * 4

The count starts at 0 and increments by 1 until all digits are added up.
For the first digit (4) count = 0, for the second digit (13) Count = 1 and for the third digit (2) count = 2
To understand why I used << Count * 4
Basically <<4 = 16
When int_Var = 2 and count = 0, 2<< 0*4 = 2<<0 = 2^0 = 2
When int_Var = 13 and count = 1, 13<< 1*4 = 13<<4 = 13*2^4 = 13*16 = 208
When int_Var = 4 and count = 2, 4<< 2*4 = 2<<8 = 4*2^8 = 4*256 = 1024

Hope this helps to understand the processes involved of converting Integer to Hexadecimal and Hexadecimal to Integer .

Martin
Attachments
Int2Hex2Int FC3 Rev1.fcf