Copying Integers to Character Array (In C)

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
TS_Death_Angel
Forum Commoner
Posts: 31
Joined: Sat Dec 31, 2005 8:49 am

Copying Integers to Character Array (In C)

Post by TS_Death_Angel »

Hi. I have an integer (4 bytes, no more, no less) which I need to copy into an unsigned character array. There has to be some simple way to do it, and if you know it, please reply. The way I'm doing it at the moment is sprintf'ing the integer to a string as a hexadecimal number, converting every two (text) bytes into an integral byte and placing it into the array. Very inefficient, and it is imperative I find a better way of doing it :<

Thanks.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Start mod(%) it from right and store it in an array.
and then
either -> cast char data type to that array.
if that doesn't work
Converts those values in the array to their ASCII Values.
And then cast char data type to that array.

I have never tried that.

In php string type casting is enough.
Post Reply