Go Down

### Topic: integer save into single bytes (Read 4567 times)previous topic - next topic

#### GekoCH

##### Mar 23, 2012, 12:05 pm
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...
Code: [Select]
`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

#### wildbill

#1
##### Mar 23, 2012, 12:33 pm
Put an 'L' on the end of 115730. Substitute the : on that line for a ;

#### ea123

#2
##### Mar 23, 2012, 02:19 pm
Hi,
another solution is defining a union:

Code: [Select]
`union Number{  long num;  byte barray[4];  } NN;`

then set the long field and get the byte array:

Code: [Select]
`  NN.num = 123466L;  byte b0 = NN.data[0];  byte b1 = NN.data[1];  byte b2 = NN.data[2];  byte b3 = NN.data[3];`

#### GekoCH

#3
##### Mar 23, 2012, 02:25 pmLast Edit: Mar 23, 2012, 02:34 pm by GekoCH Reason: 1

Hi,
another solution is defining a union:

Code: [Select]
`union Number{  long num;  byte barray[4];  } NN;`

then set the long field and get the byte array:

Code: [Select]
`  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:
Code: [Select]
`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

#### AWOL

#4
##### Mar 23, 2012, 02:38 pm
Quote
but how can I ad an 'L' to a long integer?

Just like you did here
Code: [Select]
`  NN.num = 123466L;`
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### GekoCH

#5
##### Mar 23, 2012, 02:39 pm
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...

#### GekoCH

#6
##### Mar 23, 2012, 02:42 pm
hmmm

Code: [Select]
`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'

#### AWOL

#7
##### Mar 23, 2012, 02:47 pm
I don't recommend that you use "pow" for integer work.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### PaulS

#8
##### Mar 23, 2012, 02:48 pm
Quote
Why is it working without an 'L'

Why is it working with an L where? We can't see where you call that function.

#### GekoCH

#9
##### Mar 23, 2012, 02:50 pm
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....

#### AWOL

#10
##### Mar 23, 2012, 02:51 pm
You can't add an L to a variable, the suffix is only used when specifying constants.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### GekoCH

#11
##### Mar 23, 2012, 03:25 pm

I don't recommend that you use "pow" for integer work.

So how do you suggest to decode a 32bit value?

#### AWOL

#12
##### Mar 23, 2012, 03:29 pm
Using bit shifts, multiplication/division, unions or pointers.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### GekoCH

#13
##### Mar 23, 2012, 03:56 pm
yes I tried bit shifts with:

Code: [Select]
`  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....

#### AWOL

#14
##### Mar 23, 2012, 04:37 pm
0, 8, 16,...32?

Why do your buffer subscripts start at 1?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe