convert integer to array of chars

M

Mark

Hello

I need to convert unsigned integer into an array of chars. The integer is
guaranteed to consist of no more than 4 digits. I was thinking about the way
to do this, but could not come up with any idea. Would be glad to hear any
hints. It's not a homework and I'm not asking a source code, just idea that
I could later implement.

Thanks in advance!
 
S

santosh

Mark said:
Hello

I need to convert unsigned integer into an array of chars. The
integer is guaranteed to consist of no more than 4 digits. I was
thinking about the way to do this, but could not come up with any
idea. Would be glad to hear any hints. It's not a homework and I'm
not asking a source code, just idea that I could later implement.

Thanks in advance!

If you want a text representation of the integer, just use sprintf or
snprintf. If you want to extract each byte from the integer, then you
need to mask and shift. Look-up the bit operators in any C textbook.
 
M

Malcolm McLean

Hello

I need to convert unsigned integer into an array of chars. The integer is
guaranteed to consist of no more than 4 digits. I was thinking about the way
to do this, but could not come up with any idea. Would be glad to hear any
hints. It's not a homework and I'm not asking a source code, just idea that
I could later implement.

the least significant char is x % 10, the next least significant (x/
10) % 10, and so on.

If you know that the number is always 4 decimal digits or less you can
easily hardcode all the digits.
A more general solution is to calcuate the least significant digit,
divide by ten, and repeat, stopping when the number goes to zero. You
then reverse the digits to put them into the conventional order for
reading.
 
N

Nick Keighley

I need to convert unsigned integer into an array of chars. The integer is
guaranteed to consist of no more than 4 digits. I was thinking about the way
to do this, but could not come up with any idea. Would be glad to hear any
hints. It's not a homework and I'm not asking a source code, just idea that
I could later implement.

first solve it on paper. The insight you need is to repeatedly divide
by 10

1234 / 10 -> 123 r 4
123 / 10 -> 12 r 3

etc.

in C, / does integer division and % gives you the remainder. You
should have enough clues to be able to put together a solution.
 
N

Nick Keighley

first solve it on paper. The insight you need is to repeatedly divide
by 10

1234 / 10 -> 123 r 4
123 / 10 -> 12 r 3

etc.

in C, / does integer division and % gives you the remainder. You
should have enough clues to be able to put together a solution.

oh, and to get the character corresponding to a digit

char_digit = digit + '0';

I just noticed this isn't home work! In that case I'd use sprintf!

char buff [5];
sprintf (buff, "%d", n);

real code would need some error checking
 
K

Keith Thompson

Mark said:
I need to convert unsigned integer into an array of chars. The integer
is guaranteed to consist of no more than 4 digits. I was thinking
about the way to do this, but could not come up with any idea. Would
be glad to hear any hints. It's not a homework and I'm not asking a
source code, just idea that I could later implement.

You need to specify what kind of conversion you want.
 
M

Mark

Thanks for all advices. The solution turned out to be quite simple, don't
know why I got stuck with that :)
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top