Loading...
Pages: [1]   Go Down
Author Topic: Converting a byte string to its equivalent byte value  (Read 209 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
 
I am having no end of issues with this so thought I would ask for some help guidance.

I want to convert a byte string that is send in over Serial i.e. '192' to its byte value 192. So a direct conversion as its a byte that's being sent not a string containing 192.

I have tried various methods and none seem to be working or I'm doing something wrong.

Code:

void setup () {
  Serial.begin(9600);
}

void loop(){
// Do stuff
}

void serialEvent() {
char str[3];
byte c = 0x0;
int l = 0;

while (Serial.available() > 0) {
c = Serial.read();
str[l] = c;
l++;
}

for(int i =0;i<l;i+=2) {
byte sendb = getVal(str[i]) >> 4) + getVal(str[i + 1]);
Serial.write(sendb);
}

}

byte getVal(char c)
{
   if(c >= '0' && c <= '9')
     return (byte)(c - '0');
   else if (c >= 'a' && c <= 'f')
     return (byte)(c-'a'+10)
   else
     return (byte)(c-'A'+10)
}


Above is my latest iteration which doesn't work at all tbh.

Thanks for any replies.

Modified to include full code
« Last Edit: February 18, 2013, 04:06:10 pm by tinoest » Logged

Poole, Dorset, UK
Offline Offline
God Member
*****
Karma: 12
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post the whole code not just a snippit.

What do you mean "byte string".

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to send the byte 192 via Serial to the Arduino ( atmel ) , I send this as a string. i.e "192" now I want a direct conversion so String 192 = byte 192 .
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 185
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe there's a function called atoi() for that purpose. Fairly simple to use, look it up.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe there's a function called atoi() for that purpose. Fairly simple to use, look it up.

I was under the understanding that was a string to a int.

Which I don't *think* is the same value as a byte.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Although that is actually good enough as I can do a check after to make sure its within the acceptable range 0 - 255.

Thanks!
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 185
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're right, an int is 2 bytes. However it probably work the same for most purposes. Now, understand that I never actually learned C (I just picked up what I needed), but I think you can do something like
byte b 〓 atoi(chars); or maybe
byte b 〓 (byte) atoi(chars); and it will be legal (or at least compile). Don't take my word for it though. I don't have a computer here to test it.
Logged

California
Offline Offline
Edison Member
*
Karma: 51
Posts: 2186
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was under the understanding that was a string to a int.

Which I don't *think* is the same value as a byte.

Correct, atoi() is intended for use with a string. However the only difference between a "byte string" as you refer to it and a string is that the final character in a string is a null. So if you receive the characters '1', '9', and '2'. Then you would simply need to pass the string '1', '9', '2', and '\0' to atoi() to get an int.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 336
Posts: 36476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i =0;i<l;i+=2) {
l is really a crappy global variable name. Too hard to distinguish from one. One letter global names are rarely a good idea. One letter names are usually reserved for local (loop index) variables.
Logged

Offline Offline
Sr. Member
****
Karma: 6
Posts: 413
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe a stupid question here, but if the number always fits in a byte then why send it as a string.  Just send the byte value 192 or whatever as one byte.  Then neither sender nor receiver has to do any conversions.
Logged

Pages: [1]   Go Up
Print
 
Jump to: