Converting hex to decimal...

Grogster

Senior Member
Hi there.

CHIP = 28X2

I have a uALFAT filesystem module connected to the hardware serial port, and when you query the media stats, it responds with two 32-bit HEX words. The first word is the media size, and the 2nd word is the available space left.

So, I get a response like this:

$000040000 $0003C000

Most people, myself included, cannot work this out as decimal without a calculator, so is there any code I can run in the PICAXE that will convert these kinds of responses for me, then I can just call a routine to have the conversion into decimal.
 

pete20r2

Senior Member
$0003C000 is 245760 decimal and 00111100000000000000 binary.
That is too big to fit in a word.
 
Last edited:

westaust55

Moderator
Jeremy Leach has in the past posts some 32bit routines that may assist if you do a search for 32bit maths.

I did some very specific 32bit code to convert from 4 bytes to a date and time for a 1-wire RTC chip (see in my 1-wire network thread) which may give you some ideas.
 

hippy

Technical Support
Staff member
Take a look at the code in post #6 here ...

http://www.picaxeforum.co.uk/showthread.php?t=14669

Perhaps the real question though is what you want to do with these 32-bit numbers within your program; they are the same number value whether in decimal, hex, binary or octal, it's only how they are displayed that they look different.
 

BillyGreen1973

Senior Member
$0003C000 is 15360 decimal and 00111100000000000000 binary.
That is too big to fit in a word.
I get $0003C000 to be 245760 Decimal.

Each hex digit can be represented as a 4bit binary nibble very easily, for example.

$3A= 0011 1010

You could apply this to each hex digit as it is retrieved from the scratchpad memory and using pairs of digits to make a binary byte, the 32bit word would then be 4 bytes (or 2 words w0,w1).

There would have to be some manipulation of these words but it could be done.
 
Last edited:

pete20r2

Senior Member
far out, I don't no whats wrong with mine, maybe no enough solar:confused:
yes it's (12 * 16^3) + (3 * 16^4) = 245760

Now that that is resolved,
all this depends on where you want to output this decimal number.
 

Grogster

Senior Member
Hello everyone. :)

I WANT decimal figures.

What I am doing, is showing the uSD card specs of total capacity vs remaining capacity.

Now, the uALFAT device outputs this information beautifully as the two 32-bit words mentioned above, but instead of showing 003C000, I want to be able to convert that to decimal, and show 245760.

The uSD card I am using is a 128MB one.

See the attached JPG from the manual.

Note that any conversion would then have to be multiplied by 512, or, just leave it as a sectors reference, which is fine with me - it is only to be used as a guage to how full the uSD card is.
 

Attachments

westaust55

Moderator
A thought if you only need a rough idea:
Find the highest used (1) bit in the media size value and from that shuffle both values left towards msb to remove higher zero bits.
Then use just the top word to assess how full your SD card is.
With 128MB you drop about 4 bits of data from the bottom of the values so a relatively small error in real terms.
 
Top