Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« on: March 23, 2012, 06:05:03 am » |
Hy I'm right now trying to convert an Integer ex. 115730 into a 4 byte array. So I tryed this but it doesn't work... long number = 115730: data[0] = (byte)(number >> 24); data[1] = (byte)(number >> 16); data[2] = (byte)(number >> 8); data[3] = (byte)(number);
Can someone help me, it might be just a little mistake but where.... Andy
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2348
|
 |
« Reply #1 on: March 23, 2012, 06:33:45 am » |
Put an 'L' on the end of 115730. Substitute the : on that line for a ;
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 4
Posts: 68
|
 |
« Reply #2 on: March 23, 2012, 08:19:31 am » |
Hi, another solution is defining a union: union Number { long num; byte barray[4]; } NN;
then set the long field and get the byte array: NN.num = 123466L; byte b0 = NN.data[0]; byte b1 = NN.data[1]; byte b2 = NN.data[2]; byte b3 = NN.data[3];
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #3 on: March 23, 2012, 08:25:38 am » |
Hi, another solution is defining a union: union Number { long num; byte barray[4]; } NN;
then set the long field and get the byte array: NN.num = 123466L; byte b0 = NN.data[0]; byte b1 = NN.data[1]; byte b2 = NN.data[2]; byte b3 = NN.data[3];
Hmm I thought I'm not that stupid but can you help me to implement this. What is in the data array? EDIT: Ok found something I cahnged the code to: union Number { long num; byte barray[4]; } NN;
NN.num = 115730L; byte b0 = NN.barray[0]; byte b1 = NN.barray[1]; byte b2 = NN.barray[2]; byte b3 = NN.barray[3];
Now it is working perfectly but how can I ad an 'L' to a long integer? Thx Andy
|
|
|
|
« Last Edit: March 23, 2012, 08:34:37 am by GekoCH »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: March 23, 2012, 08:38:05 am » |
but how can I ad an 'L' to a long integer? Just like you did here NN.num = 123466L;
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #5 on: March 23, 2012, 08:39:18 am » |
ok sorry did not ask the right way.
I got the number stored in a long variable eb. long test = 150216;
and now how can I add an L...
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #6 on: March 23, 2012, 08:42:12 am » |
hmmm void in32bit(long number){
union Number{ long num; byte barray[4]; } NN;
NN.num = number; byte b0 = NN.barray[0]; byte b1 = NN.barray[1]; byte b2 = NN.barray[2]; byte b3 = NN.barray[3];
Serial.println(b3, DEC); Serial.println(b2, DEC); Serial.println(b1, DEC); Serial.println(b0, DEC);
int buffer[5]; buffer[1] = b3; buffer[2] = b2; buffer[3] = b1; buffer[4] = b0;
unsigned long b11 = buffer[1] * long(pow(256,3)); unsigned long b22 = buffer[2] * long(pow(256,2)); unsigned long b33 = buffer[3] * long(pow(256,1)); unsigned long b44 = buffer[4] * long(pow(256,0)); unsigned long val = b11 + b22 +b33 + b44; Serial.println(); Serial.println(val); }
I got now this code. Why is it working without an 'L'???
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: March 23, 2012, 08:47:55 am » |
I don't recommend that you use "pow" for integer work.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35483
Seattle, WA USA
|
 |
« Reply #8 on: March 23, 2012, 08:48:22 am » |
Why is it working without an 'L'??? Why is it working with an L where? We can't see where you call that function.
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #9 on: March 23, 2012, 08:50:18 am » |
It was suggested to add an 'L' behind my long number however it is also working without since I'm not sure how I can add an 'L' behind a long variable....
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: March 23, 2012, 08:51:37 am » |
You can't add an L to a variable, the suffix is only used when specifying constants.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #11 on: March 23, 2012, 09:25:40 am » |
I don't recommend that you use "pow" for integer work.
So how do you suggest to decode a 32bit value?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: March 23, 2012, 09:29:22 am » |
Using bit shifts, multiplication/division, unions or pointers.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Switzerland
Offline
Sr. Member
Karma: 0
Posts: 261
|
 |
« Reply #13 on: March 23, 2012, 09:56:07 am » |
yes I tried bit shifts with: unsigned long b1 = buffer[1] * 1 << 32; unsigned long b2 = buffer[2] * 1 << 16; unsigned long b3 = buffer[3] * 1 << 8; unsigned long b4 = buffer[4] * 1 << 0; unsigned long val = b1 + b2 +b3 + b4;
but didn't worked therefore i switched to pow....
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19030
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: March 23, 2012, 10:37:47 am » |
0, 8, 16,...32?
Why do your buffer subscripts start at 1?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|