Converting Char array to Int array

Discussion in 'C Programming' started by Tricky, Feb 4, 2008.

  1. Tricky

    Tricky Guest

    Is there a way I can easily convert a char array into an int array,
    without having to cast each value in the array to an int and copying
    it to a new array?

    Thanks for any help :)
     
    Tricky, Feb 4, 2008
    #1
    1. Advertising

  2. "Tricky" <> wrote in message
    > Is there a way I can easily convert a char array into an int array,
    > without having to cast each value in the array to an int and copying
    > it to a new array?
    >
    > Thanks for any help :)
    >

    Basically no. On some very unusual machines sizeof(char) == sizeof(int) and
    so you can reinterpret the bits. However generally you've got to add extra
    padding within the array because it consists of two or, more usually, four
    bytes.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Feb 4, 2008
    #2
    1. Advertising

  3. Tricky

    Mark Bluemel Guest

    Tricky wrote:
    > Is there a way I can easily convert a char array into an int array,


    none other than creating the int array and assigning each char value in
    turn into it.

    > without having to cast each value in the array to an int


    casting is pointless here, as in many places. You can simply assign.

    > and copying it to a new array?



    What are you _really_ trying to achieve? chars are (smallish) integer
    values, so in some cases you may simply wish to work with them...
     
    Mark Bluemel, Feb 4, 2008
    #3
  4. Tricky

    Eric Sosman Guest

    Tricky wrote:
    > Is there a way I can easily convert a char array into an int array,
    > without having to cast each value in the array to an int and copying
    > it to a new array?


    It depends on what you mean by "convert." Please describe
    exactly what you have, and exactly what you want to get.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 4, 2008
    #4
  5. Tricky

    Tricky Guest

    On Feb 4, 4:31 pm, Eric Sosman <> wrote:
    > Tricky wrote:
    > > Is there a way I can easily convert a char array into an int array,
    > > without having to cast each value in the array to an int and copying
    > > it to a new array?

    >
    > It depends on what you mean by "convert." Please describe
    > exactly what you have, and exactly what you want to get.
    >
    > --
    > Eric Sosman
    >


    Ive read all of the data out of a Bitmap (Im ignoring anything other
    than 8 bits). Because I may want to manipulate them as if they were 10
    bit values I need to store them into something wider than a char
    array.

    No need to worry about the 10 bit thing, I know Im going to have to
    bit shift them all left by 2 once they are in the int array. Its just
    getting the values into the int array in the first place.
     
    Tricky, Feb 4, 2008
    #5
  6. Tricky

    Willem Guest

    Tricky wrote:
    ) Ive read all of the data out of a Bitmap (Im ignoring anything other
    ) than 8 bits). Because I may want to manipulate them as if they were 10
    ) bit values I need to store them into something wider than a char
    ) array.
    )
    ) No need to worry about the 10 bit thing, I know Im going to have to
    ) bit shift them all left by 2 once they are in the int array. Its just
    ) getting the values into the int array in the first place.

    If you have to bit shift them anyway, then why can't you copy them
    while you're at it ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Feb 4, 2008
    #6
  7. Tricky

    Eric Sosman Guest

    Tricky wrote:
    > On Feb 4, 4:31 pm, Eric Sosman <> wrote:
    >> Tricky wrote:
    >>> Is there a way I can easily convert a char array into an int array,
    >>> without having to cast each value in the array to an int and copying
    >>> it to a new array?

    >> It depends on what you mean by "convert." Please describe
    >> exactly what you have, and exactly what you want to get.

    >
    > Ive read all of the data out of a Bitmap (Im ignoring anything other
    > than 8 bits). Because I may want to manipulate them as if they were 10
    > bit values I need to store them into something wider than a char
    > array.
    >
    > No need to worry about the 10 bit thing, I know Im going to have to
    > bit shift them all left by 2 once they are in the int array. Its just
    > getting the values into the int array in the first place.


    Since you need each value in a form different than the
    one you read, you need to convert it explicitly to that new
    form. There's no "bulk conversion" operator.

    unsigned char orig[N];
    unsigned int new[N]; /* or maybe unsigned short? */
    int i;
    /* read orig[] values */
    for (i = 0; i < N; ++i)
    new = orig; /* or mabye orig << 2? */

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 4, 2008
    #7
  8. Tricky

    Bartc Guest

    Tricky wrote:
    > On Feb 4, 4:31 pm, Eric Sosman <> wrote:
    >> Tricky wrote:
    >>> Is there a way I can easily convert a char array into an int array,
    >>> without having to cast each value in the array to an int and copying
    >>> it to a new array?

    >>
    >> It depends on what you mean by "convert." Please describe
    >> exactly what you have, and exactly what you want to get.
    >>
    >> --
    >> Eric Sosman
    >>

    >
    > Ive read all of the data out of a Bitmap (Im ignoring anything other
    > than 8 bits). Because I may want to manipulate them as if they were 10
    > bit values I need to store them into something wider than a char
    > array.
    >
    > No need to worry about the 10 bit thing, I know Im going to have to
    > bit shift them all left by 2 once they are in the int array. Its just
    > getting the values into the int array in the first place.


    What happens to the data afterwards? If it's going back in the 8-bit array,
    you might be able to deal with it in-place without duplicating the entire
    array (which of course would take at least 2 times as much space, plus the
    original).

    Or (a long shot) you might be able to convert the original 8-bit bitmap to a
    16-bit one (zero-extend each pixel) separately. Then load it as 16-bits
    (short int) and work on it directly.

    Otherwise, convert pixel by pixel as suggested (although this would be very
    neat in a few lines of assembler if your compiler allows).

    (BTW the conversion to 10-bit by left shifting will involve a very small
    linearity error (0..255 will map to 0..1020 instead of 0..1023))

    --
    Bart
     
    Bartc, Feb 4, 2008
    #8
  9. Tricky

    CBFalconer Guest

    Tricky wrote:
    >
    > Is there a way I can easily convert a char array into an int
    > array, without having to cast each value in the array to an int
    > and copying it to a new array?


    Run the program on a machine where CHAR_BIT exceeds 15 and
    sizeof(int) is 1. :)

    Of course you can eliminate the cast on any machine.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 4, 2008
    #9
  10. Tricky

    Army1987 Guest

    CBFalconer wrote:

    > Tricky wrote:
    >>
    >> Is there a way I can easily convert a char array into an int
    >> array, without having to cast each value in the array to an int
    >> and copying it to a new array?

    >
    > Run the program on a machine where CHAR_BIT exceeds 15 and
    > sizeof(int) is 1. :)


    On the DS9K, sizeof(int) is 1, but int is two's complement and char is
    sign-and-magnitude. :)


    --
    Army1987 (Replace "NOSPAM" with "email")
     
    Army1987, Feb 5, 2008
    #10
  11. Tricky:

    > Is there a way I can easily convert a char array into an int array,
    > without having to cast each value in the array to an int and copying
    > it to a new array?
    >
    > Thanks for any help :)




    Nope, not on a system where char and int are different sizes and have
    different representations. You'd have to go with something like:

    int nums[50] = { /* numbers */ };

    char signed byte_nums[50];


    int const *src = nums;

    char signed *dest = byte_nums;
    char signed const *const destend = *(&byte_nums+1);

    do *dest++ = *src++;
    while (destend != dest);

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Feb 5, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,223
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    405
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,062
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,171
Loading...

Share This Page