Connecting Tech Pros Worldwide Help | Site Map

forcing char array to act as int array

chuck
Guest
 
Posts: n/a
#1: Feb 14 '08
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
Guest
 
Posts: n/a
#2: Feb 14 '08

re: forcing char array to act as int array


On 14 Ún, 13:45, chuck <noem...@gmail.comwrote:
Quote:
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 = "abcdefghijklmnopqrstuvwx";
int32_t* ibuffer = reinterpret_cast<int32_t*>(buffer);
Daniel T.
Guest
 
Posts: n/a
#3: Feb 14 '08

re: forcing char array to act as int array


On Feb 14, 7:45*am, chuck <noem...@gmail.comwrote:
Quote:
* 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_cast<int*>( myChar );
Otherwise, you will have to do some real work converting the raw
memory into the format you need.
Default User
Guest
 
Posts: n/a
#4: Feb 14 '08

re: forcing char array to act as int array


Daniel T. wrote:

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




Brian
peter koch
Guest
 
Posts: n/a
#5: Feb 14 '08

re: forcing char array to act as int array


On 14 Feb., 13:45, chuck <noem...@gmail.comwrote:
Quote:
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
James Kanze
Guest
 
Posts: n/a
#6: Feb 15 '08

re: forcing char array to act as int array


On Feb 14, 7:48 pm, "Daniel T." <danie...@earthlink.netwrote:
Quote:
On Feb 14, 7:45 am, chuck <noem...@gmail.comwrote:
Quote:
Quote:
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.
Quote:
Quote:
Can anyone post a simple example on how to do this?
Quote:
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?
Quote:
If so, then simply int* myArr = reinterpret_cast<int*>( myChar );
That's a good recepe for a core dump on a lot of machines.
You're forgetting alignment considerations.
Quote:
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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Closed Thread