+ Reply to Thread
Results 1 to 7 of 7

Thread: Break an int into 2 char's

  1. #1
    gualala Newbie
    Join Date
    Dec 2007
    Posts
    8

    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;
    


  2. #2
    eng1 Excellent eng1 Excellent eng1 Excellent eng1 Excellent eng1 Excellent
    Join Date
    Apr 2006
    Location
    Italy
    Posts
    938

    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
    

  3. #3
    kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent kchriste Excellent
    Join Date
    Jul 2006
    Location
    Victoria BC, Canada
    Posts
    3,681

    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.
    Inside every little problem, is a big problem trying to get out.

  4. #4
    Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent
    Join Date
    Oct 2006
    Location
    B.C., Canada
    Posts
    2,469

    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

  5. #5
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    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.

  6. #6
    Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent Torben Excellent
    Join Date
    Oct 2006
    Location
    B.C., Canada
    Posts
    2,469

    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

  7. #7
    ikalogic Good ikalogic Good ikalogic Good
    Join Date
    Dec 2003
    Location
    Limoges, France
    Posts
    632

    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/

+ Reply to Thread

Similar Threads

  1. digital speedometer using AT90S2313
    By fever in forum Micro Controllers
    Replies: 57
    Latest: 29th January 2008, 06:45 PM
  2. Supermagnets that'll break your arm..
    By Analog in forum Chit-Chat
    Replies: 11
    Latest: 5th February 2007, 07:48 PM
  3. revew code please
    By Seb in forum Micro Controllers
    Replies: 1
    Latest: 22nd February 2005, 01:15 PM
  4. Break ouy of loop possable?
    By 1Steveo in forum Micro Controllers
    Replies: 2
    Latest: 18th February 2004, 05:35 PM
  5. Can you break an led with too much voltage
    By daviddoria in forum General Electronics Chat
    Replies: 9
    Latest: 14th March 2003, 06:43 AM

Tags for this Thread