Go Down

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

#### GekoCH

#15
##### Mar 23, 2012, 04:59 pmLast Edit: Mar 23, 2012, 05:01 pm by GekoCH Reason: 1

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?

#### AWOL

#16
##### Mar 23, 2012, 05:06 pm
1 << 0 is exactly the same value as 2560
The problem lies with the progression
0, 8, 16 ... ?
"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

#17
##### Mar 23, 2012, 05:15 pm
so maybe 24 hmmmm

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

#### AWOL

#18
##### Mar 23, 2012, 05:56 pm
I'm posting this from my phone.
I can't see your results, and I'm miles from my Arduinos.
"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.

#### ea123

#19
##### Mar 23, 2012, 06:40 pm
Hi,
if you are trying to convert from byte array to long integer, simply use the union in the opposite way:

Code: [Select]
`   Number Out;   Out.barray[0] = b0;   Out.barray[1] = b1;   Out.barray[2] = b2;   Out.barray[3] = b3;   unsigned long val = Out.num`

#### GekoCH

#20
##### Mar 24, 2012, 11:41 am

Hi,
if you are trying to convert from byte array to long integer, simply use the union in the opposite way:

Code: [Select]
`   Number Out;   Out.barray[0] = b0;   Out.barray[1] = b1;   Out.barray[2] = b2;   Out.barray[3] = b3;   unsigned long val = Out.num`

Wow thx the union thing worked just great!

Finally I do need to send float numbers separated in a 4 byte array with the IEEE-754 Standard.
Converting form the bytes to float was easy but the other way round seems harder...

#### PaulS

#21
##### Mar 24, 2012, 01:56 pm
Quote
Finally I do need to send float numbers separated in a 4 byte array with the IEEE-754 Standard.
Converting form the bytes to float was easy but the other way round seems harder...

Why? It's the same union, with a float added.

#### GekoCH

#22
##### Mar 24, 2012, 05:29 pm
wow you were right hmm this union thing is quite strong have to dig deeper what else it can deliver....

Thx
Andy

#### westfw

#23
##### Mar 24, 2012, 09:04 pm

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);`

I don't see anything wrong with the original construction (aside from the ':' vs ';', which I hope is a transcription error.)
In what was does it "not work"?

#### GekoCH

#24
##### Mar 25, 2012, 05:09 pm
the value returned was wrong compared to what was expected there fore I wrote it doesn't work...

#### PaulS

#25
##### Mar 25, 2012, 05:20 pm
Quote
the value returned was wrong compared to what was expected there fore I wrote it doesn't work...

Could be that your expectations were wrong. No input, no output, no way for us to know whether the code is wrong or your expectations are wrong.

Go Up

Please enter a valid email to subscribe