April 27, 2024, 05:42:25 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


HexToDec - well almost

Started by Andy, May 27, 2016, 08:31:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

May 27, 2016, 08:31:26 AM Last Edit: May 27, 2016, 11:03:58 AM by Andy
I've managed to convert any hex value to decimal from 1 to FFFF FFFF FFFF FFF (15 F's)

Although I can't get the 16th 1 to F - the multiplier is just too big even though uint64's should take the number - they just don't.

You can change the hex value of the string to any hex value up to 16 characters, if there is an invalid hex character, or the length is greater than 16, it will return 0.

If you add an extra character i.e. FFFF FFFF FFFF FFF1 - you will get the wrong answer.

Anyway, attached is my StringMap's HexToDec function.

Setup - you can run setup_stringmap.exe - just browse to your IWBDev / EBasic root folder in C:\Program Files and click OK.

Alternatively, if that doesn't work, copy the .lib file to your Libs folder, the .inc file to your include folder.

Either way, you should be able to then compile Hex2DecV2.iwb.

Anyone have any ideas how to get that last hex value into the decimal total?

I can do it with strings from my library, but here I'm trying to get the actual value, not a string result.

Attached is the Hex2Dec program and files.







Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy


Well, in the mean time, I've extended my SMHexToDec routine.

It now converts any hex number from 1 to FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF (32 F's),
decimal value 340282366920938463463374607431768211455. 

It does of course return the value as a string, but that's the best I can do at the moment.

If the length of the hex string is < 1 or > 32, it returns zero.

It was a long tedious process as I've had to add in 480 multiplier numbers and use my own addition routine from my library.

Anyway, if you need a large hex to decimal converter - here it is!

Attached are the files...

Setup - you can run setup_stringmap.exe - just browse to your IWBDev / EBasic root folder in C:\Program Files and click OK.

Alternatively, if that doesn't work, copy the .lib file to your Libs folder, the .inc file to your include folder.

You can then compile hextodecstring.iwb, and change the string "MyString" to any hex value up to (and including) 32 characters.

Have fun.

Thanks, Andy.
 
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

jalih

Quote from: Andy on May 27, 2016, 08:31:26 AM

I can do it with strings from my library, but here I'm trying to get the actual value, not a string result.


Every hex digits is four bits, so make a lookup table to convert from ascii hex digit to binary decimal digit. Just do a table lookup for every ascii hex digit in string and shift correct binary mask in right position. Simple and fast.

Andy

May 29, 2016, 11:43:06 PM #3 Last Edit: May 30, 2016, 12:09:57 AM by Andy
Jalih,

Another good attempt at a solution and I appreciate you trying!  :)

However, you would still have to convert binary to decimal, which involves using mathematical functions.

These functions do not work on such large number so you cannot convert the large hex or binary value to a decimal value.

Even if you could get to an answer, say... 340282366920938463463374607431768211455 (16 F's - Hex), you cannot push that number into a uint64, It's simply too large.

What we really need is a "long" or "long long" variable as they have in C and the mathematical functions
( +, -, /, *, %, ^) to be able to work with this type and produce the correct results.

At the moment, I cannot see any other way except to return the decimal value as a string.

Thanks again for trying,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.