I have the following bytes that come in and I store them in a 4 byte char array:
char tmp[4] = {0x76, 0x2d, 0x0f, (byte) 0xde};
How do I get them to be:
762d0fde
so I can convert to a long int:
1982664670
What's the best way to do this?
I have the following bytes that come in and I store them in a 4 byte char array:
char tmp[4] = {0x76, 0x2d, 0x0f, (byte) 0xde};
How do I get them to be:
762d0fde
so I can convert to a long int:
1982664670
What's the best way to do this?
First, it helps if you declare the array to be "unsigned char" so you won't get any sign-extension funny business.
Then:
unsigned long bignum;
bignum = ((unsigned long)(char[0]) << 24)
| ((unsigned long)(char[1]) << 16)
| ((unsigned long)(char[2]) << 8)
| char[3];
--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html
char tmp[4] = {0x76, 0x2d, 0x0f, (byte) 0xde};
union {
char tmp[4] ;
long myLong ;
} myUnion ;
void setup() {
Serial.print(myUnion.myLong) ;
}
void loop() {}
I was thinking union too but I'm afraid his bytes are in the wrong order. The AVR architecture is little-endian so he will either have to byte-reverse the results or receive the bytes in reverse order if possible.
--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html
That worked perfect! Thanks!
Here is what I ended up with:
unsigned char tmp[4] = { 0x76, 0x2d, 0x0f, (byte) 0xde };
unsigned long sID = ((unsigned long)(tmp[0]) << 24)
| ((unsigned long)(tmp[1]) << 16)
| ((unsigned long)(tmp[2]) << 8)
| tmp[3];
Serial.print(sID, DEC);
Hmm... well I spoke too soon...
When I trace the actual bytes with: Serial.print(c, HEX);
I get the following: 48 58 FFFFFFA7 6F
so it should be: 4858A76F
Which should add up to: 1213769583
Not: 4294944623 ... which is what the code I used does.
its picking up the last 4 bytes FF FF A7 6F
So I need to check if the value is greater than 255 and mask? What is the simplest way to do that?
Maybe show us your whole code, including how you are storing the bytes.
I tried the code out using a native compiler and it works fine.
--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html
Here is the complete code... minus the standard ethernet stuff:
for (int i=0; i<4; i++){
c = client.read(); // seeking 4 spaces
}
// append the next 4 chars to buffer
char tmp[4];
for (int i=0; i<4; i++){
c = client.read();
tmp[i] = c; // seeking 4 spaces
Serial.print(c, HEX);
Serial.print(" ");
}
unsigned long sID = ((unsigned long)(tmp[0]) << 24)
| ((unsigned long)(tmp[1]) << 16)
| ((unsigned long)(tmp[2]) << 8)
| tmp[3];
Serial.print(sID, DEC);
The first print statement is returns: 48 58 FFFFFFA7 6F
And your code returns: 4294944623
When it should be: 1213769583
Geez... just noticed when I transferred the code I didn't change tmp as an "unsigned char" that seem to have fixed it.
So if want to convert or store this value as a string... how can I do that? Looks like this doesn't work:
unsigned long testID = 1716526225;
sprintf(buf, "GET /testID=%lu HTTP/1.0", testID);
I get the following results:
GET /testID=7313 HTTP/1.0
Should be:
GET /testID= 1716526225 HTTP/1.0
There's a guy here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283111301
with exactly the same problem!
unsigned long testID = 1716526225[glow]UL[/glow];
?
Yeah... that's me... broke it out incase someone was searching for the other as this topic is titled a bit differently.
I can get it working with a long... just not with a unsigned long.