Advertisements

"Tricky" <> wrote 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.

On Feb 4, 4:31 pm, Eric Sosman <> 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?
>
> It depends on what you mean by "convert." Please describe
> exactly what you have, and exactly what you want to get.
>
> --
> Eric Sosman
>

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.

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

Tricky wrote:
> On Feb 4, 4:31 pm, Eric Sosman <> 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?
>> 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.

Tricky wrote:
> On Feb 4, 4:31 pm, Eric Sosman <> 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?
>>
>> It depends on what you mean by "convert." Please describe
>> exactly what you have, and exactly what you want to get.
>>
>> --
>> Eric Sosman
>>
>
> 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))

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!