Pages: 1 [2]   Go Down
Author Topic: get single bytes from double  (Read 1272 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Shift >> 8 looks easier and portable
Agreed - I said my approach was quick and dirty.
However, for a "double" datatype, neither is truly portable, because of the different width of "double" on different platforms.
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.

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

  1. the Serial.print(hexCode, HEX); prints it in decimal

As per the documentation: http://arduino.cc/en/Serial/Println
format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

So that line of code is only telling it to print hexCode out to however many decimal places HEX converts to.  print/ln does not support outputing floats/doubles in HEX format, which is why you need to resort to some explicit conversion yourself.


  2. between 31 and EA there are twe tabspaces, which means an empty listitem is present in pointer[2]? or is this something else

I only see a single tabspace between items.  Most easily observed if you change the line to something like: Serial.print("-\t-");
But it also copies/pastes into a spreadsheet properly delimited as well.  So, why do you believe it's outputing two tabs?

  3. It does definitely not output what i want it to do, but how could i possibly solve it?

As AWOL said, what exactly do you want it to do?
Logged


Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Stop and think - tell us what you WANT to do, not how you think you should do it.

As i told in the first post, i have a value, e.g. 0x01D4629D, and i want to split this up in 0x01, 0xD4, 0x62 and 0x9D because i want to store them in eeprom memory.

the output is 4E   31   EA   4B, which is not 01 D4 62 9D. that's what i mean with different.

Quote
Quote
1. the Serial.print(hexCode, HEX); prints it in decimal
"4E   31   EA   4B" are clearly NOT decimal.

i meant the 30696092.0000000000000000, which is decimal, while i use HEX to print, 4E etc are clearly hex, but that's not what i meant.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
s i told in the first post, i have a value, e.g. 0x01D4629D
So why are you converting it to a float?
If it is meant to represent an integer, keep it as an integer type.
Converting to a floating point representation is utterly pointless, and explains why you're not seeing what you expect.
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.

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

because 0x01D4629D is bigger than the limit of an integer... an integer can only hold 0xFFFF, while a double can hold 0xFFFFFFFF, that's why i thought i had to convert it to a double.

now if i use it as an integer, it only gives the last 2 bytes:

0x01D4629D returns 62 and 9D. And that's it. so no 01 and D4, which means it floods the value of an integer
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
because 0x01D4629D is bigger than the limit of an integer... an integer can only hold 0xFFFF
Wrong.
It is bigger than an "int", but not a "long" or "unsigned long", both of which are integer data types.

If you'd told us what it was you needed to do earlier, we would all have saved a great deal of time.
« Last Edit: June 22, 2012, 08:10:21 am by AWOL » 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.

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
because 0x01D4629D is bigger than the limit of an integer... an integer can only hold 0xFFFF
Wrong.
It is bigger than an "int", but not a "long" or "unsigned long", both of which are integer data types.

thanks, i changed it to unsigned long and now it works like a charm. really thanks for that.
that's why i told in the first post that my usage of byte - int - double etc was not as it should, i wasn't sure of everything.

But now it's more clear to me, thanks
Logged


New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just to try and clarify the source of the confusion here...

You mixed what you were trying to do (save a value to eeprom), with what you were doing (storing it as a double), with no indication as to which was which.

It wasn't obvious in your initial post that storing it as a double was simply an attempt to solve the problem, as opposed to a required component of it.  That's why it's important to be clear about what you are trying to accomplish, and not how you are trying to accomplish it.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's also worth pointing out that if you assigned 1.0 to a float variable, you'd have to look long and hard at the bytes comprising the float to see anything resembling a 1, because the format is a mantissa/exponent.

So, 1.0 is represented as 0x3f800000 in 32 bit float, and 0x3ff00000000000 in 64 bit double
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.

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nevermind
« Last Edit: June 22, 2012, 09:55:34 am by Arrch » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a difference.
Shifting an int, for instance, right by 8 bits will give the most significant byte, always.
Outputting the bytes based on a pointer to the variable will yield different results, based on the endianess of the processor.
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.

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a difference.
Shifting an int, for instance, right by 8 bits will give the most significant byte, always.
Outputting the bytes based on a pointer to the variable will yield different results, based on the endianess of the processor.
Didn't click in my head initially, now it does. Thanks.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As long as you're just breaking the variable into bytes to store then as long as you put them back into the original order it doesn't matter what endianess the system uses. So the Q&D way is portable.

I would suggest something like this untested bit:
Code:
byte *chunk = ( byte * ) &varToStore;
int *EEaddy = EEPROMaddress;
byte i = 0;

while ( i < sizeof(varToStore)) // while loop being slightly faster than for-next
{
  EEPROM.write( EEaddy++, *(chunk++) ); // incrementing pointers being faster than indexing
  i++; 
}

Let me know what I got wrong. It's been a while since I did much with pointers.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: