system
March 23, 2012, 11:05am
1
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
Put an 'L' on the end of 115730. Substitute the : on that line for a ;
system
March 23, 2012, 1:19pm
3
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];
system
March 23, 2012, 1:25pm
4
ea123:
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
system
March 23, 2012, 1:38pm
5
but how can I ad an 'L' to a long integer?
Just like you did here NN.num = 123466L;
system
March 23, 2012, 1:39pm
6
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...
system
March 23, 2012, 1:42pm
7
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'???
system
March 23, 2012, 1:47pm
8
I don't recommend that you use "pow" for integer work.
system
March 23, 2012, 1:48pm
9
Why is it working without an 'L'???
Why is it working with an L where? We can't see where you call that function.
system
March 23, 2012, 1:50pm
10
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....
system
March 23, 2012, 1:51pm
11
You can't add an L to a variable, the suffix is only used when specifying constants.
system
March 23, 2012, 2:25pm
12
AWOL:
I don't recommend that you use "pow" for integer work.
So how do you suggest to decode a 32bit value?
system
March 23, 2012, 2:29pm
13
Using bit shifts, multiplication/division, unions or pointers.
system
March 23, 2012, 2:56pm
14
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....
system
March 23, 2012, 3:37pm
15
0, 8, 16,...32?
Why do your buffer subscripts start at 1?
system
March 23, 2012, 3:59pm
16
AWOL:
Why do your buffer subscripts start at 1?
Since in the first ([0]) is the adress where the message came from...
is 1 << 0 not the same as pow(256,0).
Or how can I do it differently?
system
March 23, 2012, 4:06pm
17
1 << 0 is exactly the same value as 2560
The problem lies with the progression
0, 8, 16 ... ?
system
March 23, 2012, 4:15pm
18
so maybe 24 hmmmm
But still no success or is it still wrong (24)?
system
March 23, 2012, 4:56pm
19
I'm posting this from my phone.
I can't see your results, and I'm miles from my Arduinos.
system
March 23, 2012, 5:40pm
20
Hi,
if you are trying to convert from byte array to long integer, simply use the union in the opposite way:
Number Out;
Out.barray[0] = b0;
Out.barray[1] = b1;
Out.barray[2] = b2;
Out.barray[3] = b3;
unsigned long val = Out.num