Number Conversion & ASCII Look-up

westaust55

Moderator
In view of needing to reference past posts and/or copy along some information to guide those wishing to learn about conversion between decimal, hexadecimal, binary and also looking for ASCII code lookup,
I have created the attached table to help.

EDIT: latest version of the Number Conversion & ASCII lookup table is now at Post 20
Use this link:
http://www.picaxeforum.co.uk/showthread.php?t=10893&p=91966
 
Last edited:

kevrus

New Member
Westaust, a very useful table especially for anyone who is not familiar with all the numbering systems, well worth downloading a copy...
 

bob weir

New Member
numbers list

if you ever have occasion to update the list would you add a column of BCD numbers ?
recent work with the DS1307 has shown how confusing they can be vs. hex numbers.
especially as for many instances they are the same.
 

westaust55

Moderator
I am toying with whether adding a BCD column would cause confusion for many.

To try and add BCD to my table would have limitations. With all the other number formats, all values on the row are equivalent between the columns whereas the BCD would only relates to say the decimal value.

I think that if you get to grasp with BCD as a concept it will all fall into place.

a BCD number uses 4 bits per digit so with 8 bits we can only have 0 to 99.

For decimal 09 the BCD is $09 = %0000 1001
For decimal 19 the BCD is $19 = %0001 1001
For decimal 27 the BCD is $27 = %0010 0111
For decimal 55 the BCD is $55 = %0101 0101
For decimal 88 the BCD is $88 = %1000 1000
For decimal 99 the BCD is $99 = %1001 1001

Hopefully you can see the ease of the patterns above
 

BeanieBots

Moderator
I agree westaust, I can only see BCD values causing confusion. Don't actually see any advantage that could be gained. Converting BCD to say decimal is just the WRONG way to think about such a numbering system. Converting to/from HEX requires no conversion. At least, non that would make sense in a lookup table.
 

BCJKiwi

Senior Member
Should also be noted that;

1. The standard windows calculator with the view changed to scientific will convert between hex/dec/octal/binary.

2. Any character other than the standard number/upper/lower case letters, and some of the punctuation, will vary with the language/character set in use.
 

Mycroft2152

Senior Member
I agree, a BCD listing could be confusing.

But a short description of what BCD is and a couple of examples, would be helpful.

Myc
 

bob weir

New Member
BCD or not BCD

some background. the request is based on working with the RTC chip = DS1307.
this chip requires that you give it the time with sec, min , hr. etc. in BCD format.
the picaxe intruction set is optimized with special i2c commands to talk to this IC.
and when you ask it the time the reply sent back are numbers in BCD format.

to take these numbers and display them onto your LCD to show a clock forces
you to have to convert BCD to hex. the math is tricky. but do-able with some
effort. it was easy to get lost in having to think in this other number format .
that's why a master hex - binary - decimal - BCD list would have been useful

whether westaust55 adds in new columns or not is ok as for now i can take his fine chart
and scissors, tape & a scanner and make what is wanted. the question at this
point is where to put the new BCD column ? between decimal & hex or between hex & binary ?

you are all invited to take the 1307 coding path , or remember when you did,
and once you connect with that ... let us know if the 'confusion' of having them
on the list is still a valid objection.
 
Last edited:

BeanieBots

Moderator
The "confusion" is that you should NOT be converting the numbers to anything such as decimal. It would not make sense to do anything like that.
Think of the numbers in HEX.

34 in BCD is 34 in hex is 3 and 4 in decimal. Anything else would lead to confusion.
Lets say you get $34 (BCD) back from the seconds register. You instantly know it is 34 seconds. Converting that to "52" in decimal does nothing but confuse. If you want the two seperate numbers, just mask off the relevant nibble. If it's the left digit, shift it right. (divide by 16).

How would knowing that 34 seconds ($34 BCD or $34 hex) was 52 in decimal help as apposed to confuse?
 

BeanieBots

Moderator
My post was to distinguish between "BCDtoASCII" as a software command and BCD to ASCII as in a lookup table.

For the propsers of adding BCD to the table,
How does knowing the decimal value help? (I may well have missed something)
 

westaust55

Moderator
I have gone down the path suggested by Mycroft and added some notes and examples relating the the BCD numbering system.

As clarified, the confusion Bob is experiencing is not in the simple conversion between bases, but how to extract the two separate 4 bit nibbles and create two separate numbers as ASCII representations which are then sent to the LCD display.

That is straight forward with many examples on the forum of the required maths.

1. take the BCD number and divide by 16 to move it into the lower nibble (4 bits) then add decimal 48 (hexadecimal $30) to create the ASCII representation for the ten's digit and send to the LCD.
2. take the original BCD number then AND with 15 ($0F) to mask/remove the top 4 bits. Next add decimal 48 to create the ASCII representation for the units digit and sent to the LCD

The above represents the fundamental steps reuqiuired.

Alternatively as BCJKiwi has stated above the BCDTOASCII command can be used to achieve the same result.


EDIT: latest version of the Number Conversion & ASCII lookup table is now at Post 20
 
Last edited:

fred_b

Member
don't you need to convert to decimal for ...

I am just starting out so forgive me if I am way off here.

I think the difficulty comes when you need to do addition or check for < or >

It is easy to test-

if b0=$12 then noon

or

if b0<>$12 then main

but if you want

if b0 <$45 then main

It seems like you would need to do some converting first.

to convert the $45 from the clock into the decimal number 45, this code can be used:

'converts clock value to the decimal value
readi2c 0,(b6,b5,b4) 'clock reading for b5 is $45 (minutes)
bcdtoascii b5,b1,b2
let b3=b1*10-480+b2-48 ' b3 is decimal 45
if b0<b3 then main

by converting the BCD reading, the number can easily be added to other decimal numbers or tested to be less than or greater than a decimal number.
 

BeanieBots

Moderator
fred_b,
You are making this much more complicated than it needs to be.
Taking your example of $45.

To compare for < or >, simply do the comparison with the number in BCD.
if you want to compare b0 (holding the 45 minutes) with forty six minutes, then it's as this
if b0 < $46 then.... (similar for >).

If you want to convert $45 (BCD) into decimal, don't go through the conversion to ASCII first. Just convert direct to decimal.

$45 is four lots of 16 plus 5.
The first digit can be extracted simply by dividing by 16.
$45/16=4
The second digit can be extracted by masking.
$45 AND $0F = 5
If these values are held in variables then just re-combine to get the decimal value. (4X10+5=45)

say b0=$45

b1=b0/16 'result=4
b1=b1*10 'result=40
b2=b0 and $0F 'result=5
b3=b1+b2 'result=45

The above can be simplified further but I've shown each step for clarity.
 

vttom

Senior Member
FYI - I can see where it might make sense to do BCD->Decimal rather than BCD->ASCII so that you can do "sertxd(#b0)".
 

westaust55

Moderator
The value for 128 is incorrect, it should be %10000000 not %01110000 I believe?
Yes, you are correct Silent Screamer.
A typo has slipped through there :eek:.
I will fix the original table and update soon (rather than leave an incorrect table).
In the throws of moving house so may not be an immediate change.
 

SilentScreamer

Senior Member
Good, its a great table I only just found it a matter of days ago and I've already started using it loads and finding its helping learn the conversions. (I even now understand why it works (I always just accepted it) in 8, 16, 32, 64, 128, 256, 512 and 1024 :D)
 
Top