How can i get 2 bytes of an int? please help
-
- Posts: 270
- Joined: 29 Jan 2010 16:36
How can i get 2 bytes of an int? please help
Hi there. This may sound like a simple question but i'm unable to find an easy way of seperating two bytes of an unsigned int variable. I want to store each byte into an unsigned char so that i can store this value into the EEPROM or can i simply store the whole unsigned int into the EEPROM's two successive locations?
-
- Posts: 270
- Joined: 29 Jan 2010 16:36
Re: How can i get 2 bytes of an int? please help
Just found that i can use Lo() and Hi() of the built_in.h to separate the bytes. which took me a long time to figure out.
-
- Posts: 1179
- Joined: 24 Nov 2005 20:07
- Location: Colorado, USA
Re: How can i get 2 bytes of an int? please help
Here is another method to achieve the same result... by using a Union. This has the benefit of allowing access to high or low bytes independently and to the full word when needed. It also has the added benefit of not having to rely on bit shifting as found in other methods.
Create the Union template somwhere before "void main ()":
Next create the instance where you need to use the Union in a function and load it with data:
This same kind of union can also work the other way around when you want to combine 2-bytes into 1 word (16-bits). This technique is handy for reading 16-bit timer registers or ADC registers... ect... basically anytime two bytes are read out and are needed as a combined word.
Create the Union template somwhere before "void main ()":
Code: Select all
union U16_
{
unsigned int word; // For accessing the whole 16-bit unsigned int
unsigned char byte[2]; // For accessing 16-bits as individual bytes
};
Code: Select all
void WriteMyEE_Bytes (unsigned int MyInWord)
{
union U16_ Int2Bytes;
...
Int2Bytes.word = MyInWord;
EEPROM_WR (Int2Bytes.byte[1]); // write the hi byte
EEPROM_WR (Int2Bytes.byte[0]); // write the lo byte to eeprom
...
}
Code: Select all
unsigned int MakeMyWord (unsigned char HiInbyte, unsigned char LoInbyte)
{
union U16_ Bytes2Int;
...
...
Bytes2Int.byte[1] = HiInbyte; // combine bytes
Bytes2Int.byte[0] = LoInbyte; //
...
return (Bytes2Int.word); // return the combined bytes as a 16-bit word
}
-
- Posts: 270
- Joined: 29 Jan 2010 16:36
Re: How can i get 2 bytes of an int? please help
Thanks Sparky. Will sure try your approach as well. But don't you think just using lo(), hi(), higher() and highest() is fastest possible way? its like you don't need more than one line of code to get a byte.
- ranko.rankovic
- Posts: 433
- Joined: 11 Jun 2010 09:22
Re: How can i get 2 bytes of an int? please help
Hello underworldman,
These Built-in Routines are made for easier and faster code writing. So in fact, for your need this is right and fast solution. All of these routines are detail explained in our help file.
Best regards
These Built-in Routines are made for easier and faster code writing. So in fact, for your need this is right and fast solution. All of these routines are detail explained in our help file.
Best regards
Ranko Rankovic
mikroElektronika [Support Department]
mikroElektronika [Support Department]
-
- Posts: 1179
- Joined: 24 Nov 2005 20:07
- Location: Colorado, USA
Re: How can i get 2 bytes of an int? please help
In programming there are always different ways to arrive at the same solution. Using a union is one way, Hi/Lo commands are another. As Ranko said using the Hi/Lo commands are a fast and easy solution to do what you want and a perfect solution for programmers with less experience.
However this does come at a cost, that being the Hi/Lo commands are compiler specific. So if you ever decided to port this code over to another processor platform which uses a different tool set, all Hi/Lo commands will have to be re-coded to work correctly. From a code size or execution speed perspective I don't know which is better because I never did a comparison. Might be an interesting test to try.
The real benefit of the union approach is the ability to perform two processes with one command. That is the ability to combine 2-bytes into 1-word, or take 1-word and split it into 2-bytes. No compiler specific commands are needed. The only portability issue that one may encounter with the union technique is the byte order in which the data is stored in memory. In the case of PIC's it's little endian first i.e byte[0] = lowbyte, byte[1]= highbyte. With some compiler and processor platforms this order is reversed. From the programmers perspective this is easily accomodated by assigning the data to the correct locations.
However this does come at a cost, that being the Hi/Lo commands are compiler specific. So if you ever decided to port this code over to another processor platform which uses a different tool set, all Hi/Lo commands will have to be re-coded to work correctly. From a code size or execution speed perspective I don't know which is better because I never did a comparison. Might be an interesting test to try.
The real benefit of the union approach is the ability to perform two processes with one command. That is the ability to combine 2-bytes into 1-word, or take 1-word and split it into 2-bytes. No compiler specific commands are needed. The only portability issue that one may encounter with the union technique is the byte order in which the data is stored in memory. In the case of PIC's it's little endian first i.e byte[0] = lowbyte, byte[1]= highbyte. With some compiler and processor platforms this order is reversed. From the programmers perspective this is easily accomodated by assigning the data to the correct locations.
-
- Posts: 270
- Joined: 29 Jan 2010 16:36
Re: How can i get 2 bytes of an int? please help
Thanks for your explanation Sparky and Ranko