Converting Char array to Int array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tricky

    Converting Char array to Int array

    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 :)
  • Malcolm McLean

    #2
    Re: Converting Char array to Int array


    "Tricky" <Trickyhead@gma il.comwrote 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.


    Comment

    • Eric Sosman

      #3
      Re: Converting Char array to Int array

      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
      esosman@ieee-dot-org.invalid

      Comment

      • Tricky

        #4
        Re: Converting Char array to Int array

        On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
        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
        esos...@ieee-dot-org.invalid
        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.

        Comment

        • Willem

          #5
          Re: Converting Char array to Int array

          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

          Comment

          • Eric Sosman

            #6
            Re: Converting Char array to Int array

            Tricky wrote:
            On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
            >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[i] = orig[i]; /* or mabye orig[i] << 2? */

            --
            Eric Sosman
            esosman@ieee-dot-org.invalid

            Comment

            • CBFalconer

              #7
              Re: Converting Char array to Int array

              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

              Comment

              • Army1987

                #8
                Re: Converting Char array to Int array

                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")

                Comment

                • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=

                  #9
                  Re: Converting Char array to Int array

                  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

                  Comment

                  Working...