Thread: two char to one int

  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
    28,413
    Thinking in terms of bits, you can perform a bit shift on one char and bitwise and the result with the other char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,059
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,834
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,834
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02: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, 06: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, 01:30 PM