2 bytes into integer solution

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.

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.

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.

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.

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

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?

PaulS:

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!

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

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 :). 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 :fearful: i appreciate all the posters time and answers and sharing of knowledge.

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

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.