forcing char array to act as int array

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

    forcing char array to act as int array

    Hello,
    I am trying to 'force' a character array to act as an int array. I
    want to use 4 chars to store an actual int , in the array, not a number
    converted to a string. So every 4 characters of my array is really a
    full integer value.

    Can anyone post a simple example on how to do this?

    Thanks,
    chuck

  • Ondra Holub

    #2
    Re: forcing char array to act as int array

    On 14 Ún, 13:45, chuck <noem...@gmail. comwrote:
    Hello,
    I am trying to 'force' a character array to act as an int array. I
    want to use 4 chars to store an actual int , in the array, not a number
    converted to a string. So every 4 characters of my array is really a
    full integer value.
    >
    Can anyone post a simple example on how to do this?
    >
    Thanks,
    chuck
    You need int type, where sizeof() is 4. Then:

    char* buffer = "abcdefghijklmn opqrstuvwx";
    int32_t* ibuffer = reinterpret_cas t<int32_t*>(buf fer);

    Comment

    • Daniel T.

      #3
      Re: forcing char array to act as int array

      On Feb 14, 7:45 am, chuck <noem...@gmail. comwrote:
        I am trying to 'force' a character array to act as an int array.  I
      want to use 4 chars to store an actual int , in the array, not a number
      converted to a string.  So every 4 characters of my array is really a
      full integer value.
      >
      Can anyone post a simple example on how to do this?
      Are the ints in your system sizeof( 4 )? Is the memory in the char
      array formatted with the same endian as what the system uses?

      If so, then simply int* myArr = reinterpret_cas t<int*>( myChar );
      Otherwise, you will have to do some real work converting the raw
      memory into the format you need.

      Comment

      • Default User

        #4
        Re: forcing char array to act as int array

        Daniel T. wrote:

        Are the ints in your system sizeof( 4 )?
        They'd have to be, wouldn't they?




        Brian

        Comment

        • peter koch

          #5
          Re: forcing char array to act as int array

          On 14 Feb., 13:45, chuck <noem...@gmail. comwrote:
          Hello,
            I am trying to 'force' a character array to act as an int array.  I
          want to use 4 chars to store an actual int , in the array, not a number
          converted to a string.  So every 4 characters of my array is really a
          full integer value.
          >
          Can anyone post a simple example on how to do this?
          >
          Thanks,
          chuck
          Do it the other way around: regard your integer variable as an array
          of four (if that is the size of your int) characters and std::copy to/
          from the ints.

          /Peter

          Comment

          • James Kanze

            #6
            Re: forcing char array to act as int array

            On Feb 14, 7:48 pm, "Daniel T." <danie...@earth link.netwrote:
            On Feb 14, 7:45 am, chuck <noem...@gmail. comwrote:
            I am trying to 'force' a character array to act as an int array. I
            want to use 4 chars to store an actual int , in the array, not a number
            converted to a string. So every 4 characters of my array is really a
            full integer value.
            Can anyone post a simple example on how to do this?
            Are the ints in your system sizeof( 4 )? Is the memory in the
            char array formatted with the same endian as what the system
            uses?
            If so, then simply int* myArr = reinterpret_cas t<int*>( myChar );
            That's a good recepe for a core dump on a lot of machines.
            You're forgetting alignment considerations.
            Otherwise, you will have to do some real work converting the
            raw memory into the format you need.
            No real work, just a bit of shifting and masking.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...