Decimal To ASCII

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Decimal To ASCII

Post by Widgetman » Tue Apr 01, 2008 12:56 am

Hi,
I am trying to figure out how to convert a decimal number to it's Ascii equivalent. If I have a number say 1432345 I want to convert it to 31 34 33 32 33 34 35

Does anyone know a easy way to do this ?
Any help would be greatly appreciated.
thanks

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Apr 01, 2008 12:36 pm

There are a number of ways to do this, here is one...

Code: Select all

Include "utils.bas"
Include "usart.bas"
Include "convert.bas"

Dim Value As Byte

SetBaudrate(br115200)
Value = Digit(1432345,2) + $30
USART.write("$", Hextostr(value),13,10)

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Tue Apr 01, 2008 7:16 pm

Hi Dave,
Thanks for the reply, but I am trying to convert the other way. I have a string of numbers say "1407123" and I want to convert each digit to a ASCII equiv. 1 = 31, 4 = 34, 0 = 30, 7 = 37, 2 = 32, 3 = 33
Maybe I am not seeing the clear picture here, but I can't seem to find a way to parse the string and get the charcter I want to determine what the ASCII value would be. Any help would be appreciated.
Thanks

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Apr 01, 2008 7:33 pm

That's easier...

Code: Select all

dim Ch as char
Ch = Str(index)
or

Code: Select all

dim Value as byte
Value = byte(Str(index))
This will display the values...

Code: Select all

Include "usart.bas"
Include "convert.bas"

dim Str as string
dim Index, Value as byte

SetBaudrate(br115200)
Str = "1407123"
index = 0
while Str(index) <> null
   Value = byte(Str(index)) 
   USART.Write("$", HexToStr(Value) ,13,10)
   inc(index)
wend  

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Tue Apr 01, 2008 7:36 pm

Thanks a Bunch Dave,
I will play with that

Post Reply