Pages: [1] 2   Go Down
Author Topic: integer save into single bytes  (Read 2455 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Online Online
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put an 'L' on the end of 115730. Substitute the : on that line for a ;
Logged

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
another solution is defining a union:

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

then set the long field and get the byte array:

Code:
  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 Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
another solution is defining a union:

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

then set the long field and get the byte array:

Code:
 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:
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 Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but how can I ad an 'L' to a long integer?
Just like you did here
Code:
  NN.num = 123466L;
Logged

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

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hmmm

Code:
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 Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 616
Posts: 49437
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Switzerland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yes I tried bit shifts with:

Code:
  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 Offline
Brattain Member
*****
Karma: 302
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2   Go Up
Jump to: