---
+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Member level 2
    Points: 928, Level: 6
    brucelee2's Avatar
    Join Date
    Aug 2009
    Location
    on a PCB
    Posts
    49
    Helped
    5 / 5
    Points
    928
    Level
    6

    Two 8-bit chars into a 16-bit number

    Hi,

    I have a HMI which sends two 8-bit chars which represent a 16-bit touch screen coordinate.
    So in an example, the HMI sends:
    0X03 then 0X0E (both seperate) which represent the coordinate 030E (782).

    How can i combine the two into a single variable to represent the number 782? I was thinking of creating a 'int' and shifting each into it some how.

    ie.

    char first = 0x03;
    char second = 0x0E;
    int number;

    number=first;
    number=number<<8;
    ---Now im stuck---



    Any Ideas?
    Thanks a lot.:D

    •   Alt1st August 2010, 12:17

      advertising

        
       

  2. #2
    FvM
    FvM is offline
    Super Moderator
    Points: 172,207, Level: 98
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    27,924
    Helped
    8808 / 8808
    Points
    172,207
    Level
    98

    Two 8-bit chars into a 16-bit number

    C has bitwise OR operation for similar purposes
    Code:
    number = (int)first << 8 | second;
    Declaring a union is another common way, some compilers have special functions for it, e.g. CCS C make16().


    1 members found this post helpful.

  3. #3
    Member level 2
    Points: 928, Level: 6
    brucelee2's Avatar
    Join Date
    Aug 2009
    Location
    on a PCB
    Posts
    49
    Helped
    5 / 5
    Points
    928
    Level
    6

    Two 8-bit chars into a 16-bit number

    Oh ok. Im using MikroC, so ill consult the manual to see if there is such a function in the compiler.

    thanks again



  4. #4
    Advanced Member level 2
    Points: 5,617, Level: 17
    Kurenai_ryu's Avatar
    Join Date
    Jun 2006
    Location
    Bolivia
    Posts
    571
    Helped
    127 / 127
    Points
    5,617
    Level
    17

    Two 8-bit chars into a 16-bit number

    better if you declare all unsigned (unsigned char and unsigned int) on data declaration and data casting... so

    Code:
    unsigned char first = 0x03;
    unsigned char second = 0x0E;
    unsigned int number; 
    
    ...
    
    
    number = (unsigned int)first << 8 | second;
    will have less problems...
    {problem arise when you cast a signed char to int like 0xe0 for 'second', will be casted to 0xFFE0 and not 0x00E0... try it!}


    1 members found this post helpful.

    •   Alt2nd August 2010, 17:44

      advertising

        
       

  5. #5
    Advanced Member level 3
    Points: 5,367, Level: 17

    Join Date
    Jul 2009
    Posts
    734
    Helped
    81 / 86
    Points
    5,367
    Level
    17

    Two 8-bit chars into a 16-bit number

    alternatively, you can use union - which requires no coding on your part.

    or a macro:
    Code:
    #define WORD(msb, lsb)  (((msb) << 8) | (lsb))
    Code:
    number = (unsigned int)first << 8 | second;
    you probably want to use more brackets there to prevent the '|' operator to be executed before the left shift.


    1 members found this post helpful.

  6. #6
    FvM
    FvM is offline
    Super Moderator
    Points: 172,207, Level: 98
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    27,924
    Helped
    8808 / 8808
    Points
    172,207
    Level
    98

    Re: Two 8-bit chars into a 16-bit number

    you probably want to use more brackets there to prevent the '|' operator to be executed before the left shift.
    Only if you're not aware of ANSI C operator precedence rules, I think.
    problem arise when you cast a signed char to int like 0xe0 for 'second'
    It's not correctly handled by all compilers, but arithmetic type conversion is required for bitwise OR by the specification. Thank you for the correction.


    1 members found this post helpful.

  7. #7
    Advanced Member level 1
    Points: 2,875, Level: 12

    Join Date
    Apr 2010
    Location
    UK
    Posts
    457
    Helped
    74 / 74
    Points
    2,875
    Level
    12

    Re: Two 8-bit chars into a 16-bit number

    As you use mikroC

    unsigned int myInt ;

    hi(myInt) = 0x03 ;
    lo(myInt) = 0x0E ;

    Its whole lot easier that way.

    jack



  8. #8
    Full Member level 4
    Points: 2,910, Level: 12
    saeed_pk's Avatar
    Join Date
    May 2006
    Location
    Islamabad, Pakistan
    Posts
    236
    Helped
    35 / 35
    Points
    2,910
    Level
    12

    Re: Two 8-bit chars into a 16-bit number

    you can store First(Low Byte) at the base address of the int variable then store second byte @ (int base address +1) then call/use 16 bit integer
    use these macros

    #define Low(c) (*(unsigned char *)&(c))
    #define High(c) (*((unsigned char *)&(c)+1))

    char a=0x30;
    char b=0x53;
    int data;
    Low(data)=a;
    High(data)=b;

    now use data as 16 bit number

    Hope you find the answer



  9. #9
    Newbie level 1
    Points: 485, Level: 4

    Join Date
    Mar 2010
    Location
    HK
    Posts
    1
    Helped
    0 / 0
    Points
    485
    Level
    4

    Re: Two 8-bit chars into a 16-bit number

    use the Union,like below:
    union
    {
    char a[2];
    int b;
    }number;

    number.a[0]=0x03;number.a[1]=0x0E;

    then, number.b=0x030E,
    it just costs two bytes of ram , not four bytes.



  10. #10
    Newbie level 1
    Points: 466, Level: 4

    Join Date
    May 2010
    Location
    Belarus
    Posts
    1
    Helped
    0 / 0
    Points
    466
    Level
    4

    Re: Two 8-bit chars into a 16-bit number

    if you have some free bits? try this:

    number = 0;
    number |=first;
    number<<8;
    number |=second;



+ Post New Thread
Please login