Thread: Question regarding integers and chars

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Question regarding integers and chars

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char * c = new char[1];
        *(c+0) = 0x10;
        *(c+1) = 0x11;
        unsigned int i = *(c);
        cout << i;
        return 0;
    }
    So, with the given code, we can say that c[0]=0x10 and c[1]=0x11.
    My question is, how could I combine both values into a single integer(2bytes)?
    What I want is my program to output 4113 (0x1011)

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Don't create a new thread with the same question because you aren't getting an answer. That is a very sure way of getting both threads locked. Now the size of int, short, ect. are implementatino specific so be sure to verify your implementation. Note my use of short instead of unsigned int.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char * c = new char[1];
        short result=0;
    
        *(c+0) = 0x10;
        *(c+1) = 0x11;
        
        for(short i = 0; i < sizeof(i); i++)
    	result = (result << 8) + c[i];
    
        short j = 0x1011;
        cout << result << " " <<j;
        
    
        cin.get();
    }
    Last edited by AndrewHunter; 07-02-2011 at 06:01 PM. Reason: Fixed spacing
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This question is actually slightly different to the other.

    If we ignore the problem of writing two characters of a one character array, the value of i in this case will always be 0x10
    Last edited by grumpy; 07-02-2011 at 06:11 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. Question about strings n chars
    By cszym001 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:09 PM
  3. Simple question about array of chars
    By Chewy in forum C Programming
    Replies: 9
    Last Post: 04-12-2004, 05:13 AM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. Transforming Integers into Chars
    By N8760 in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 01:58 PM