Searching \ for 'Ascii to binary conversion' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=ascii+binary+conversion
Search entire site for: 'Ascii to binary conversion'.

Truncated match.
PICList Thread
'Ascii to binary conversion'
1996\07\12@133550 by mike

flavicon
picon face
Hi,

In an upcoming project I will need to convert an ascii string of
numbers to a 4 byte binary equivalent.

The string format is:

 123.4567890

As the format is fixed I can ignore the decimal point, which leaves
me with the job of converting a string like 1234567890 to binary.

In principle, assuming I have a 4 byte accumulator, I could:

 clr  acc
 add  0 * 1 to acc
 add  9 * 10 to acc
 add  8 * 100 to acc
 add  7 * 1000 to acc
 ... etc

This seems long winded and therefore slow. Do any of you guys have
any other solutions?


Thanks for your help,



Mike

1996\07\12@164953 by Doug Manzer

picon face
At  5:55 PM 7/12/96 +0000, Mayes uk wrote:

>In an upcoming project I will need to convert an ascii string of
>numbers to a 4 byte binary equivalent.

The "old" version of Microchip's AN575 had a C program that
does something like this. However, it required a natural logarithm
calculation (see pp.5-15 & 5-16 of the 1994/95 _Embedded Control
Handbook_).

Anyone know how to implement log() on a PIC?

Regards, D.M.

1996\07\12@174158 by Martin J. Maney

flavicon
face
On Fri, 12 Jul 1996, Mayes uk wrote:

> In principle, assuming I have a 4 byte accumulator, I could:
>
>   clr  acc
>   add  0 * 1 to acc
>   add  9 * 10 to acc
>   add  8 * 100 to acc
>   add  7 * 1000 to acc
>   ... etc
>
> This seems long winded and therefore slow. Do any of you guys have
> any other solutions?

On a machine with a four-byte accumulator and hardware multiply, I'd
probably suggest the obvious simplification

 0 + 10 * (9 + 10 * (8 + ...

Implemented, of course, in a loop with the body

 res = 10 * res + value_of_next_digit

I suppose you could manage that, making the "10 * res" work byte by
byte.  In fact, I'm inclined to think that might be the best solution as
a balance between speed and code size (he said rashly, having only
sketched a few alternatives in his head).

I'll stop now before I get myself into serious trouble.  ;-)

1996\07\12@180328 by fastfwd

face
flavicon
face
Mayes uk <spam_OUTmikeTakeThisOuTspamd-m-g.demon.co.uk> wrote:

> In an upcoming project I will need to convert an ascii string of
> numbers to a 4 byte binary equivalent.
> ....
> As the format is fixed I can ignore the decimal point, which leaves
> me with the job of converting a string like 1234567890 to binary.
>
> In principle, assuming I have a 4 byte accumulator, I could:
>
>   clr  acc
>   add  0 * 1 to acc
>   add  9 * 10 to acc
>   add  8 * 100 to acc
>   add  7 * 1000 to acc
>   ... etc
>
> This seems long winded and therefore slow. Do any of you guys have
> any other solutions?

Mike:

For doing general-purpose conversions like this, it'd be better to
start from the LEFT end of the string and do something like this:

   1. Clear the accumulator.
   2. Multiply the accumulator by 10.
   3. Add the next digit to the accumulator.
   4. If any digits have not yet been added, loop back to step 2.

Tracing the first few iterations of this algorithm...

   1.  Acc = 0.
   2.  Acc = 0 * 10 = 0.
   3.  Acc = 0 + 1 = 1.
   4.  Loop back to Step 2.
   2.  Acc = 1 * 10 = 10.
   3.  Acc = 10 + 2 = 12.
   4.  Loop back to step 2.
   2.  Acc = 12 * 10 = 120.
   3.  Acc = 120 + 3 = 123.
   4.  Loop back to Step 2.
   etc....

This is slow, too, but nowhere near as bad as starting from the
right-hand side and multiplying by 1, 10, 100, 1000, etc.

You can write a moderately-fast "multiply-by-10" subroutine by doing
something like this:

   temp = x << 2
   temp = temp + x
   x = temp << 1

The "<<" operator means "shift left"; "x << 2", therefore, means "x,
shifted left two bit-positions".

-Andy

P.S.  One more thing... If your string really IS ASCII, don't forget
to subtract 48 from each digit's representation before adding it to
your accumulator.

Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499

1996\07\14@024039 by Rich Leggitt

flavicon
face
The only other option I can think of would be to:

 clr acc
 add 1 to acc
 multiply acc by 10
 add 2 to acc
 multiply acc by 10
 etc.

By the way, don't forget that 10X = 8X+2X, that is, 10X = (X SHL 3)+(X SHL 1).
This can sometimes give better performance than using a multiply instruction.

At 05:55  07/12/96 GMT, Mayes uk wrote:
{Quote hidden}

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...