two char to one int

This is a discussion on two char to one int within the C Programming forums, part of the General Programming Boards category; i am writing a c program using mplab and pic16f887, with the pickit2. I have a tc77, which outputs a ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    two char to one int

    i am writing a c program using mplab and pic16f887, with the pickit2. I have a tc77, which outputs a temperature in 16 bits. I have it outputing two sets of 8 bits each, and each set will go to char a or char b. I need to take char a and char b and combine them, but not add them.

    Example:
    Char a: 0000 0101
    Char b: 1011 0111
    I need to take both char's, and put them to an int so that int c looks like this:
    Int c: 0000 0101 1011 0111

    I cant figure out exactly how to do this.
    Any ideas?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Thinking in terms of bits, you can perform a bit shift on one char and bitwise and the result with the other char.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Figured it out, its

    c = (a << 8) + b;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by murph909 View Post
    Figured it out, its

    c = (a << 8) + b;
    Or, quite simply ... c = (a * 256) + b;

    Either way...

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by murph909 View Post
    Figured it out, its

    c = (a << 8) + b;
    Better to stay with bitwise operations i.e. logical addition instead of arithmetic.

    just my 2c

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by murph909
    Figured it out, its

    c = (a << 8) + b;
    Yeah, that looks correct. By the way, I made a mistake by mentioning bitwise and instead of bitwise or, but your use of addition bypasses my mistake.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Why it works at all is mysterious. I would have thought shifting an 8-bit quantity left 8 positions would make it zero. Yet somewhere a conversion to int is implied. It happens BEFORE any '+' would naturally cast it to match types. Can someone shed light?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by nonoob
    Why it works at all is mysterious. I would have thought shifting an 8-bit quantity left 8 positions would make it zero. Yet somewhere a conversion to int is implied. It happens BEFORE any '+' would naturally cast it to match types. Can someone shed light?
    When a bit shift is performed, the integer promotions (to int or unsigned int) are performed on the operands.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Thanks laserlight. I must have forgotten about some of the magic that goes on behind the scenes.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Thanks laserlight. I must have forgotten about some of the magic that goes on behind the scenes.
    As long as the lvalue is an int... it will process the rvalues as ints...
    (oops, just saw that Lase beat me to it... oh well)

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by CommonTater
    As long as the lvalue is an int... it will process the rvalues as ints...
    What exactly are you referring to when you say lvalue and rvalues? After all, in this context a and b could well be lvalues.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  2. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  3. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 05:18 PM
  4. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21