Go Down

### Topic: 2 bytes into integer solution (Read 856 times)previous topic - next topic

#### grendle

##### Mar 15, 2013, 03:38 pmLast Edit: Mar 15, 2013, 04:05 pm by grendle Reason: 1
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: [Select]
`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 9600int z;z = (int)(word(x, y));`

corrected: bytes hex values, had it backwards, apologies.

#### Arrch

#1
##### Mar 15, 2013, 03:44 pm
Code: [Select]
`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.

#### PaulS

#2
##### Mar 15, 2013, 03:45 pm
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.

#### AlxDroidDev

#3
##### Mar 15, 2013, 03:47 pmLast Edit: Mar 15, 2013, 03:56 pm by AlxDroidDev Reason: 1
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).

Some of my projects:
Shield for DS1337+, DS1624 and AT24C1024B (RTC, temp & mem): http://forum.arduino.cc/index.php/topic,126197.0.html
CHDK Camera remote shutter (BT, IR, USB): http://forum.arduino.cc/index.php?topic=295377.0

#### PaulS

#4
##### Mar 15, 2013, 03:52 pm
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?

#### AlxDroidDev

#5
##### Mar 15, 2013, 03:57 pm

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!
Some of my projects:
Shield for DS1337+, DS1624 and AT24C1024B (RTC, temp & mem): http://forum.arduino.cc/index.php/topic,126197.0.html
CHDK Camera remote shutter (BT, IR, USB): http://forum.arduino.cc/index.php?topic=295377.0

#### grendle

#6
##### Mar 15, 2013, 04:01 pm
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 . 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  i appreciate all the posters time and answers and sharing of knowledge.

#### Arrch

#7
##### Mar 15, 2013, 04:08 pm

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.

#### grendle

#8
##### Mar 15, 2013, 04:16 pm
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.

Go Up

Please enter a valid email to subscribe