Connecting Tech Pros Worldwide Help | Site Map

Converting Char array to Int array

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

re: Converting Char array to Int array



"Tricky" <Trickyhead@gmail.comwrote in message
Quote:
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

Eric Sosman
Guest
 
Posts: n/a
#3: Feb 4 '08

re: Converting Char array to Int array


Tricky wrote:
Quote:
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
Tricky
Guest
 
Posts: n/a
#4: Feb 4 '08

re: Converting Char array to Int array


On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Quote:
Tricky wrote:
Quote:
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.
Willem
Guest
 
Posts: n/a
#5: Feb 4 '08

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
Eric Sosman
Guest
 
Posts: n/a
#6: Feb 4 '08

re: Converting Char array to Int array


Tricky wrote:
Quote:
On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Quote:
>Tricky wrote:
Quote:
>>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
CBFalconer
Guest
 
Posts: n/a
#7: Feb 4 '08

re: Converting Char array to Int array


Tricky wrote:
Quote:
>
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

Army1987
Guest
 
Posts: n/a
#8: Feb 5 '08

re: Converting Char array to Int array


CBFalconer wrote:
Quote:
Tricky wrote:
Quote:
>>
>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")
=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#9: Feb 5 '08

re: Converting Char array to Int array


Tricky:
Quote:
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
Closed Thread


Similar C / C++ bytes