Mayes 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 - fastfwd@ix.netcom.com Fast Forward Engineering, Vista, California http://www.geocities.com/SiliconValley/2499