Pages: [1]   Go Down
Author Topic: 2 bytes into integer solution  (Read 485 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i thought id share this with anyone interested, i didnt want to post and ask 'how do you convert 2 bytes into an integer', its probably easy and i should find the answer myself. well, it was easy, finding the answer took me a litte time though, although ive learned a lot more along the way. but, to save someone time, this worked for me.

Code:
byte x = 0x25 //i used hex values to see if i can store 9600, baud, into an eeprom. just as a test.
byte y = 0x80 // hex, 0x2850 is 9600
int z;
z = (int)(word(x, y));

corrected: bytes hex values, had it backwards, apologies.
« Last Edit: March 15, 2013, 10:05:35 am by grendle » Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte x = 28 //i used hex values to see if i can store 9600, baud, into an eeprom. just as a test.
byte y = 50 // hex, 0x2850 is 9600

Compiler assumes you mean 28 and 50 decimal, not hex. The numbers should have a 0x in front of them.
Logged

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

Multiplying the MSB by 256 and adding the LSB doesn't require any magic function.

Shifting the MSB left by 8 and adding the LSB achieves the same result, and requires no magic function.

Learning HOW the magic functions work is better than stumbling over the magic functions.
Logged

BHZ, MG, Brazil
Offline Offline
Sr. Member
****
Karma: 10
Posts: 346
Android developer; Arduino enthusiast
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually 9600 is 0x2580, and not 0x2850

This operation is a lot faster if you just do this:

byte x = 0x25;  // If you don't preffix with "0x", it will assume decimal, not hex
byte y = 0x80;
int z;
z = ( x << 8 ) + y;

Using bit shift operations is a lot faster than typecasting everything. Multiplying x by 256 also works, and IDK if it is faster or not than the bit shifting operation (but I suppose bit shifting is easier on the microcontroller).

« Last Edit: March 15, 2013, 09:56:46 am by AlxDroidDev » Logged

Learn to live: Live to learn.
Showing off my work: http://arduino.cc/forum/index.php/topic,126197.0.html

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

Quote
but I suppose bit shifting is easier on the FPU
Your Arduino has a floating point unit? That's a pretty rare add on. Where did you get it?
Logged

BHZ, MG, Brazil
Offline Offline
Sr. Member
****
Karma: 10
Posts: 346
Android developer; Arduino enthusiast
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I suppose bit shifting is easier on the FPU
Your Arduino has a floating point unit? That's a pretty rare add on. Where did you get it?

corrected!
Logged

Learn to live: Live to learn.
Showing off my work: http://arduino.cc/forum/index.php/topic,126197.0.html

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Actually 9600 is 0x2580

This operation is a lot faster if you just do this:

byte x = 0x25;
byte y = 0x80;
int z;
z = ( x << 8 ) + y;

yes, sorry was coding from memory got it backwards, but actually this was what i was originally trying to do, and i couldnt get it to work. the excercise was just putting 2 bytes together, sorry if i didnt use x=0x28. and..

Quote
Multiplying the MSB by 256 and adding the LSB doesn't require any magic function.

Shifting the MSB left by 8 and adding the LSB achieves the same result, and requires no magic function.

Learning HOW the magic functions work is better than stumbling over the magic functions.

i am not looking for a magic function, im new and seriously trying to learn the proper way to do things. i understand many people come to the forums for you guys to do their work for them, but im interested in learning how to fish, so you dont have to keep buying me dinner Paul smiley. rest assured all the answers here are going to messed with by me over next few hours until i thoroughly understand them and can apply them where needed from memory. my memory that is not eeprom  smiley-eek-blue i appreciate all the posters time and answers and sharing of knowledge.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, sorry was coding from memory

One tip for the future is to NEVER code from memory if you're asking for help. People will spend more time fixing the mistakes that are caused by coding from memory than the actual issue you are having.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
One tip for the future is to NEVER code from memory if you're asking for help. People will spend more time fixing the mistakes that are caused by coding from memory than the actual issue you are having.

yes your correct, i apologize for this, wont happen again. sorry to posters, op corrected.
Logged

Pages: [1]   Go Up
Jump to: