integer save into single bytes

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 ;

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];

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

but how can I ad an 'L' to a long integer?

Just like you did here   NN.num = 123466L;

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...

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'???

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

Why is it working without an 'L'???

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

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....

You can't add an L to a variable, the suffix is only used when specifying constants.

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

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

Using bit shifts, multiplication/division, unions or pointers.

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....

0, 8, 16,...32?

Why do your buffer subscripts start at 1?

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?

1 << 0 is exactly the same value as 2560
The problem lies with the progression
0, 8, 16 ... ?

so maybe 24 hmmmm :slight_smile:

But still no success or is it still wrong (24)?

I'm posting this from my phone.
I can't see your results, and I'm miles from my Arduinos.

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