Thread: Chary array of 4 to Int

  1. #1
    Registered User
    Join Date
    Jul 2003

    Chary array of 4 to Int

    Hi, I have a chary array of

    chararray[4] = {B4,F1,C3,45};
    I want to change that to the int 45C3F1B4. I know that with MAKEWORD I can do 2 of them, but in this case I got 4, is there a macro for 4?

  2. #2
    Registered User
    Join Date
    Nov 2001
    One solution is via bitwise operators. You'll need to do stuff like shifting, AND, and OR.


  3. #3
    Join Date
    Jul 2005
    One good way is:

    (*chararray + (chararray[1] << CHAR_BIT) + (chararray[2] << (2 * CHAR_BIT)) + (chararray[3] << (3 * CHAR_BIT)))
    One bad way is:
    (*((long *) chararray))
    This way's bad because it assumes little-endian order, and that's definitely an assumption you should not be making. But it's there.

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    i got a better way!!! (well different way)..

    cast the char to an int and asign it to an int.. then since the numbers begin from 48 (0) to 57(9)(On the ASCII thing) all you do is subtract 48 from the int where the number has been assigned..
    but you say... well thats only 1 digit..
    multiply the number by 10 and do the same thing again but this time with the 2nd char in the array.

    aint i a genius?
    My Website
    Add Color To Your Code!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Actually that's a crappy way to do it. There's nothing in the standard that states that zero has to have the decimal value of 48. Furthermore, since it is in fact a char going to an int, there is no need to cast it. It'll automaticly be promoted.

    Hm... Actually, it depends on what the origional poster was talking about. If they have four bytes, that regardless of their value, are to be used to assemble a four byte integer, then no, it wouldn't work anyway.

    However, if each byte in the array were to represent a single digit in a new number, which is only four digits long, it would work. Then, if this were the case, to make it more portable, you'd simply replace the value 48 which you used, with the '0' character. Like so:
    int main()
        char array[] = { '1', '2', '3', '4' };
        int number;
        for( int x = number = 0; x < 4; x++ )
            number *= 10;
            number += array[ x ] - '0';
        std::cout << number << std::endl;
        return 0;

    Of course you could have fun with unions...
    union foo
        int bar;
        unsigned char bytes[ sizeof( int ) ];
    Or memcpy...
    memcpy( &myint, myarray, sizeof( int ) );
    Last edited by quzah; 07-20-2005 at 09:03 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Join Date
    Jul 2005
    Quote Originally Posted by mrafcho001
    aint i a genius?
    No. You could try actually reading the question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM