Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 30th April 2008, 01:56 PM   (permalink)
Default Break an int into 2 char's

Hi,
I'm writing some codes in C18. I have an unsigned int that I wish to break into two chars. I saw something on union struct for a byte, but I don't know how to change it into int. Could anyone help? Thanks.

I wish I can have a variable, say "Value",
Value = 0x8FFF;
Value.HighByte // gives 0x8F
Value.LowByte // gives 0xFF
or any other way that works?

The code I saw for char, so that we can use something like Flags = 0; to set all to zeros or Flags.Timeout = 1; to set a specific value.
Code:
union
{
  struct
  {
    unsigned Timeout:1;
    unsigned None:7;
  } Bit;
  unsigned char Byte;
} Flags;
gualala is offline   Reply With Quote
Old 30th April 2008, 02:58 PM   (permalink)
Default

Quote:
Originally Posted by gualala
Hi,
I'm writing some codes in C18. I have an unsigned int that I wish to break into two chars. I saw something on union struct for a byte, but I don't know how to change it into int. Could anyone help? Thanks.

I wish I can have a variable, say "Value",
Value = 0x8FFF;
Value.HighByte // gives 0x8F
Value.LowByte // gives 0xFF
or any other way that works?
I would do a right-shift and use casts.
Code:
unsigned char low_byte = Value;    // cast
unsigned int tmp = Value>>8;       // right-shift
unsigned char high_byte = tmp;     // cast
eng1 is offline   Reply With Quote
Old 1st May 2008, 03:49 AM   (permalink)
Default

You can do it eng1's way with shifts or this way with unions:
Code:
union {
	unsigned int Integer;
	struct{
		unsigned char LowByte;
		unsigned char HighByte;
		};
	} Value;

	Value.Integer = 0x1234;
	// Value.HighByte == 0x12;
	// Value.LowByte == 0x34;
The advantage of the union is you can operate directly on the Bytes of the Integer.
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline   Reply With Quote
Old 1st May 2008, 05:17 AM   (permalink)
Default

Or you could do it with a little pointer arithmetic if you like:

Code:
#include <stdio.h>

int main(void)
{
    unsigned int value = 0x8FFF;
    unsigned char *low_byte = (unsigned char *) &value,
        *high_byte = low_byte + 1;

    printf("0x%04x => Low: 0x%02x; High: 0x%02x\n", value, *low_byte, *high_byte);

    return 0;
}
This also allows you access to the individual bytes, but just remember this: http://xkcd.com/371/


Torben
Torben is online now   Reply With Quote
Old 1st May 2008, 06:33 AM   (permalink)
Default

Quote:
Originally Posted by Torben
Or you could do it with a little pointer arithmetic if you like:

Torben
Are we sure that C18 stores ints in little endian format.

Mike.
Pommie is online now   Reply With Quote
Old 1st May 2008, 06:42 AM   (permalink)
Default

Quote:
Originally Posted by Pommie
Are we sure that C18 stores ints in little endian format.

Mike.
Nope! Just going with what the OP posted, figuring s/he'd just swap the labels if not. Checking the C18 User's Guide confirms it though (page 12).


Torben
Torben is online now   Reply With Quote
Old 2nd May 2008, 10:05 AM   (permalink)
Default

Quote:
Originally Posted by Torben
This also allows you access to the individual bytes, but just remember this: http://xkcd.com/371/
Torben
That's orriginal! loooooooool
__________________
Ibrahim Kamal
check my electronics and robotics page: http://www.ikalogic.com/
ikalogic is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
digital speedometer using AT90S2313 fever Micro Controllers 57 29th January 2008 06:44 PM
Supermagnets that'll break your arm.. Analog Chit-Chat 11 5th February 2007 07:47 PM
revew code please Seb Micro Controllers 1 22nd February 2005 01:14 PM
Break ouy of loop possable? 1Steveo Micro Controllers 2 18th February 2004 05:35 PM
Can you break an led with too much voltage daviddoria General Electronics Chat 9 14th March 2003 06:42 AM



All times are GMT. The time now is 06:25 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.