char to byte conversion

Hello,

I am trying to send some binary data to my Arduino remotely, but I need the data to be retrieved from a web page. So the web page contains values like 0x61,0x00,0x0c,0x94,0x7e,...
I can receive the data from the page as a sequence of char variables or String objects but I am not sure how to convert them back to bytes.

It is like having the following:

char a = '0x61';
int bytevalue = ... //missing this step
Serial.print(bytevalue, BYTE);

I have tried casting but does not give the right output:

Serial.print((int)a, BYTE);
//I need Serial.print(0x61, BYTE);

Any help would be much appreciated,

Thanks!

Use:-
Serial.write(a);

Grumpy_Mike:
Use:-
Serial.write(a);

Does not work, when I try the following I get in Serial monitor two different things: 1a

char a = '0x61';
Serial.write(a);
Serial.write(0x61);

Does not work,

Yes it does.

char a = '0x61';

Is a rubbish statement, you should use

char a = 0x61;

no single quotes

Grumpy_Mike:

Does not work,

Yes it does.

char a = '0x61';

Is a rubbish statement, you should use

char a = 0x61;

no single quotes

I know char a = 0x61; works fine, but maybe I was not very clear: the sketch reads the byte values from a web page using the Ethernet library using the read() function like:

char c = client.read(); which I think will return something like char a = '0x61'; and not char a = 0x61;

Code:
char c = client.read();which I think will return something like
Code:
char a = '0x61';

Think all you like, but '0x61' is a four character multicharacter constant, and could not possibly be returned by "client.read();"

which I think will return something like
Code:

char a = '0x61';

No that statement is meaningless. The single quote defines a single ASCII character, I have no idea what that statement returns but it is not what you are expecting.
You need to find out what form the Ethernet library read() function returns.

No that statement is meaningless. The single quote defines a single ASCII character, I have no idea what that statement returns but it is not what you are expecting.
You need to find out what form the Ethernet library read() function returns.

Thanks! You are so right about the single quotes, really missed it! The read() returns int values, so reads it single character from the page and returns it into an int.
So now the question is how to convert '0', 'x', '6', '1' to 0x61 byte value!

Ignore the first two bytes. The real value of the next two is 0x36 and 0x31, so to concatenate them use:-

v1 = 0x36;
v2 = 0x31;

value = ( (v1 & 0xf)<< 4) | (v2 & 0xf);

Psst, Mike, what about 0x4f? (for example) :wink:

Psst, Mike, what about 0x4f? (for example)

Yes but he didn't ask about that did he. :smiley:

Some reading material:

http://www.google.com/search?q=convert+hex+site%3Ahttp%3A%2F%2Farduino.cc%2Fforum%2Findex.php&hl=en&num=100&lr=&ft=i&cr=&safe=images

Grumpy_Mike:

Psst, Mike, what about 0x4f? (for example)

Yes but he didn't ask about that did he. :smiley:

That's just mean! How about using sscanf() instead? Or something like this:

char hexdig(char ch) {
  if (ch >= '0' && ch <= '9') return ch - '0';
  if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
  if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
  return -1;
}

int string_to_byte(char *data) {
  if (data[0] != '0' || data[1] != 'x') return -1;
  int a = hexdig(data[2]);
  if (a < 0) return -1;
  int b = hexdig(data[3]);
  if (b < 0) return -1;
  return (a << 4) + b;
}

Usage:

char *str = "0xfe";
int val = string_to_byte(str);

There are also functions in the standard library to do this, if you can afford to link that in. Look at strtol() and sscanf().

Grumpy_Mike:

Psst, Mike, what about 0x4f? (for example)

Yes but he didn't ask about that did he. :smiley:

Oooo..., spin control! :slight_smile:

jwatte:

Grumpy_Mike:

Psst, Mike, what about 0x4f? (for example)

Yes but he didn't ask about that did he. :smiley:

That's just mean! How about using sscanf() instead? Or something like this:

char hexdig(char ch) {

if (ch >= '0' && ch <= '9') return ch - '0';
  if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
  if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
  return -1;
}

int string_to_byte(char *data) {
  if (data[0] != '0' || data[1] != 'x') return -1;
  int a = hexdig(data[2]);
  if (a < 0) return -1;
  int b = hexdig(data[3]);
  if (b < 0) return -1;
  return (a << 4) + b;
}




Usage:



char *str = "0xfe";
int val = string_to_byte(str);




There are also functions in the standard library to do this, if you can afford to link that in. Look at strtol() and sscanf().

Thanks a lot! That worked pretty well! It also worked with:

str = "0x1d";
int n;
sscanf (str,"%x",&n);
Serial.prinln(n, BYTE);