ASCII to Binary Conversions?
Benj
Posts: 66
I have a project that requires calculating a checksum. I need to take 1-12 ASCII characters, convert them to binary, then do some addition, xor, and a few other manipulations, and then turn the result into ASCII again. Is this possible with the Prop, and if so, where should I start looking for examples of ASCII to BIN conversion?
Benj
Benj
Comments
If "sum" is your binary value which is initialized to zero and "char" is the ASCII code for the digit, then you could write:
sum := sum * 10 + char - "0"
You would need to put this in a loop that would take the ASCII codes, left to right, one at a time and run them through this statement.
Your code would also need to check to make sure the ASCII codes are for digits only and maybe stop at the first non-digit.
The Extended FullDuplexSerial object in the Propeller Object Exchange does this sort of thing.
The checksum method I've used here at work is
While this is in C-ish, you should be able to "spin" it. "xtoa" stands for "Convert a hex digit to ASCII".
--Rich
There's nothing special, or arcane, about ASCII. The ASCII standard is simply an agreement among people: "Hey, guys, when we have a character to send, let's send it as an eight-bit number. See, we can use 00110000 for the printable character "3", and 00100000 for a blank, and like that." Much of the world has said, "Cool. We'll follow that standard."
Much of the world, but not all. There are other codes, such as EBCDIC, that assign character meanings to eight-bit numbers in a different way.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i@arrl.net
1) Perform the 8 bit sum of all the ASCII characters sent in the data field.
2) Fold the resulting eight bit sum into six bits by XOR'ing the two MSB's of the sum with the two LSB's of the sum such that the new D1 is the old D1 XOR D7 and the new D0 is the old D0 XOR D6.
3) The resulting lower six bits are then masked off, producing a code range of from 00 to 3F hex.
4) This is then added to the ASCII code for '0' (30 hex), generating the final checksum character.
I can do it by hand, but would like to do it with a Prop. So far I have been able to do step 1. Here is my code, using "H0" as the data:
An example from the manual:
The following is an example of a typical exchange:
Host sends query to get back On-Board Module version information
Flag Data Field Checksum Terminator
ASCII [noparse][[/noparse]$] [noparse][[/noparse]@] [noparse][[/noparse]CR]
Hex [noparse][[/noparse]24] [noparse][[/noparse]40] [noparse][[/noparse]31] [noparse][[/noparse]0D]
Checksum of hex 40 data field:
Bits 76543210
Sum = 40 = 01000000
Bits 7 and 6_
aligned for_
X0R 00000001
X0R result 01000001
Mask D5,D0 00000001
Addend "0" 00110000
Final Chk 00110001 Hex 31, ASCII "1"