Converting Char array to Int array

T

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 :)
 
M

Malcolm McLean

Tricky said:
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.
 
M

Mark Bluemel

Tricky said:
Is there a way I can easily convert a char array into an int array,

none other than creating the int array and assigning each char value in
turn into it.
without having to cast each value in the array to an int

casting is pointless here, as in many places. You can simply assign.
and copying it to a new array?


What are you _really_ trying to achieve? chars are (smallish) integer
values, so in some cases you may simply wish to work with them...
 
E

Eric Sosman

Tricky said:
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.
 
T

Tricky

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.
 
W

Willem

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
 
E

Eric Sosman

Tricky said:
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 = orig; /* or mabye orig << 2? */
 
B

Bartc

Tricky said:
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.

What happens to the data afterwards? If it's going back in the 8-bit array,
you might be able to deal with it in-place without duplicating the entire
array (which of course would take at least 2 times as much space, plus the
original).

Or (a long shot) you might be able to convert the original 8-bit bitmap to a
16-bit one (zero-extend each pixel) separately. Then load it as 16-bits
(short int) and work on it directly.

Otherwise, convert pixel by pixel as suggested (although this would be very
neat in a few lines of assembler if your compiler allows).

(BTW the conversion to 10-bit by left shifting will involve a very small
linearity error (0..255 will map to 0..1020 instead of 0..1023))
 
C

CBFalconer

Tricky said:
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.
 
A

Army1987

CBFalconer said:
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. :)
 
T

Tomás Ó hÉilidhe

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top