go away spammer

Author Topic: Explanation of "strip off ASCII"  (Read 2697 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Explanation of "strip off ASCII"
« on: February 18, 2009, 10:08:47 AM »
In the Blackfin source code that Admin wrote up , there is the following line
Code: [Select]
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
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: Explanation of "strip off ASCII"
« Reply #1 on: February 18, 2009, 10:23:01 AM »
In the Blackfin source code that Admin wrote up , there is the following line
Code: [Select]
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

Code: [Select]
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
Code: [Select]
  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) for more info.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Explanation of "strip off ASCII"
« Reply #2 on: February 18, 2009, 10:34:39 AM »
Why does it strip off the ASCII ? Whats the reason for it?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Explanation of "strip off ASCII"
« Reply #3 on: February 18, 2009, 11:21:33 AM »
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?

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Explanation of "strip off ASCII"
« Reply #4 on: February 18, 2009, 11:32:30 AM »
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
Code: [Select]
X_left=response[6]*100
If its not for ASCII , whats it for? It does something to the data that the Blackfin camera transmits.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Explanation of "strip off ASCII"
« Reply #5 on: February 18, 2009, 12:41:21 PM »
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

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Explanation of "strip off ASCII"
« Reply #6 on: February 18, 2009, 12:52:01 PM »
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==
00x300x00
10x310x01
20x320x02
30x330x03
40x340x04
50x350x05
60x360x06
70x370x07
80x380x08
90x390x09

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Explanation of "strip off ASCII"
« Reply #7 on: February 18, 2009, 01:49:05 PM »
but what about characters like A , B , C ?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Explanation of "strip off ASCII"
« Reply #8 on: February 18, 2009, 01:56:11 PM »
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
Code: [Select]
X_left=response[6]*100which means that its a number
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: Explanation of "strip off ASCII"
« Reply #9 on: February 18, 2009, 02:02:22 PM »
A = 01000001 = 0x41



01000001
&
00001111
=
00000001
 
Would also come out as 0x01 ?


http://www.theskull.com/javascript/ascii-binary-list.html

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: Explanation of "strip off ASCII"
« Reply #10 on: February 18, 2009, 02:11:33 PM »
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.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: Explanation of "strip off ASCII"
« Reply #11 on: February 18, 2009, 02:23:12 PM »
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:
Code: [Select]
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.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Explanation of "strip off ASCII"
« Reply #12 on: February 18, 2009, 02:30:53 PM »
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:
Code: [Select]
int digit = asciiDigit - '0';
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list