Society of Robots - Robot Forum
Software => Software => Topic started by: airman00 on February 18, 2009, 10:08:47 AM
-
In the Blackfin source code that Admin wrote up , there is the following line
response[counter]=temp & 0x0F;//store values into an array, &0x0F strips ascii off to use in char stringsWhat does it mean to strip off the ASCII to use in char strings?
Does it mean that it makes a received character usable for the character-type variable?
Like if I send the letter 'M' as ASCII, it will convert it to an 'M' that can be stored into a character-type variable?
Thanks
-
In the Blackfin source code that Admin wrote up , there is the following line
response[counter]=temp & 0x0F;//store values into an array, &0x0F strips ascii off to use in char stringsWhat does it mean to strip off the ASCII to use in char strings?
Does it mean that it makes a received character usable for the character-type variable?
Like if I send the letter 'M' as ASCII, it will convert it to an 'M' that can be stored into a character-type variable?
Thanks
response[counter]=temp & 0x0F;
It's a binary "and" operation. It takes each bit of its operands and perform a binary and with them.
For instance, the M character is 0100 1101, 0x0F is 0000 1111
0100 1101
& 0000 1111
-------------
0000 1101
In this case, what it actually does is force the for most significant bits to 0 (binary mask)
look at http://en.wikipedia.org/wiki/Mask_(computing) (http://en.wikipedia.org/wiki/Mask_(computing)) for more info.
-
Why does it strip off the ASCII ? Whats the reason for it?
-
That comment is not right. ASCII is 7 bits long, not 4 bits long; As chelmi said, binary AND-ing with 0x0F strips off the most significant 4 bits of the char, so why would the original coder say it's stripping off ASCII? A character type variable is actually a 8 bit variable so it hold more then ASCII - again, no reason to strip anything off!
What does the original code do with the "response" array later on?
-
What does the original code do with the "response" array later on?
Uses the variable later on to do actions , control servos, etc.
Heres one example
X_left=response[6]*100
If its not for ASCII , whats it for? It does something to the data that the Blackfin camera transmits.
-
My guess is that its converting ASCII digits into their binary equivalents.
ie ASCII '0' (as typed on your keyboard) is stored as '0x30' so the AND with 0x0f will convert it to 0x00
equally: '1', '2', '3' ...... '9' will be converted to 0x01, 0x02, 0x03, ...... 0x09
-
I was drawing up this pretty table while WebBot sent his answer.
I actually never new this technique, I was looking throw the ASCII table looking for "candidates" for stripping the first nibble of the byte. Nice!
| ==ASCII== | ==Before== | ==After== |
| 0 | 0x30 | 0x00 |
| 1 | 0x31 | 0x01 |
| 2 | 0x32 | 0x02 |
| 3 | 0x33 | 0x03 |
| 4 | 0x34 | 0x04 |
| 5 | 0x35 | 0x05 |
| 6 | 0x36 | 0x06 |
| 7 | 0x37 | 0x07 |
| 8 | 0x38 | 0x08 |
| 9 | 0x39 | 0x09 |
-
but what about characters like A , B , C ?
-
Well upper case characters like 'A' start at hex 0x41. Anding them with 0x0f wont make much sense as the AND process will end up with a number in the range 0x00 .... 0x0F ie 16 combinations. Since there are more than 16 upper case letters you will end up with duplicates.
Thats why I think its being applied to numbers, not characters. This is confirmed by your comment
X_left=response[6]*100which means that its a number
-
A = 01000001 = 0x41
01000001
&
00001111
=
00000001
Would also come out as 0x01 ?
http://www.theskull.com/javascript/ascii-binary-list.html
-
A = 01000001 = 0x41
01000001
&
00001111
=
00000001
Would also come out as 0x01 ?
http://www.theskull.com/javascript/ascii-binary-list.html
Yes, but it does not really make sense to use this as a number.
-
I understand whats going on now....
Yea its used to convert the ASCII into regular numbers
Most of the data is numbers , so thats why its used.
and then when the camera transmits "123" , its really sending three bytes - "1" "2" "3" , all in ASCII.
Then the code strips it off and makes it into a regular number.
Then it translates those individual numbers into the real byte , using this code:
Y_mean=response[8]*100 + response[9]*10 + response[10];
Basically it translates "123" which is 3 bytes, into 123 which is only one byte.
-
This is quite a common technique. Especially in the early days of RS232 where certain characters were used for 'flow control' - especially Xon/Xoff. This meant that data had to be sent over the wire as 7 bit ASCII. So the sender would turn a number liker 123 into "123" for transmission and the receiver would convert it back to 123. The 'AND' procedure is just a quick way of saying:
int digit = asciiDigit - '0';