468,491 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

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 :)
Feb 4 '08 #1
8 10507

"Tricky" <Tr********@gmail.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.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 4 '08 #2
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
es*****@ieee-dot-org.invalid
Feb 4 '08 #3
On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
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.
Feb 4 '08 #4
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
Feb 4 '08 #5
Tricky wrote:
On Feb 4, 4:31 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>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
es*****@ieee-dot-org.invalid
Feb 4 '08 #6
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

Feb 4 '08 #7
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")
Feb 5 '08 #8
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
Feb 5 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt melton | last post: by
4 posts views Thread by Joseph Suprenant | last post: by
4 posts views Thread by jagmeena | last post: by
16 posts views Thread by x | last post: by
8 posts views Thread by Ramiro Barbosa, Jr. | last post: by
18 posts views Thread by No Such Luck | last post: by
4 posts views Thread by Prabhu | last post: by
18 posts views Thread by Marcio Kleemann | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.