# Getting every digit of a decimal number

This topic is 4968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm looking to be able to get an integer and print it into a string as a decimal number. And I'm writing the function myself, because I'm messing around with writing a small OS, and I'll probably need this, before I port a C library (if ever). So, here's what I have so far:
int twocomp(int num)
{
return (~num) + 1;
}

char digit(int num)
{
char* digits = "0123456789abcdef";

if( num > 15 || num < 0 )
return -1;

return digits[num];
}

void sprintDec(char* s, int num)
{
int tenmod = 10;        ///< To keep track of next biggest number for mod
int prev_tenmod = 0;    ///< To keep track of the previous mod number
int i,j;                        ///< String counters
char neg = 0;           ///< If number is negative

if( num & 0x8000000 ){  // if negative number
// perform 2's complement on number to get positive number, then add - sign
num = twocomp(num);
neg = 1;
}

// we're print the numbers backwards. The 1's, 10's, 100's at the left,
// and the 10000's, 100000's at the right
for(i=0; tenmod < 0x7fffffff; prev_tenmod = tenmod, tenmod *= 10, i++ ){
if( tenmod > 10 )
s = digit( ((num%tenmod)-(num%prev_tenmod))/prev_tenmod );
else
s = digit( (num % tenmod) );

printf("\n i: %d, tenmod: %d", i, tenmod);
}

// add the negative sign and null-terminator
if( neg)        s = '-';
s[i+1] = '\0';

// reverse string
// every iteration, we inc j, dec i, and swap the character at both places.
// Eventually, they will be right next to each other, or on the same character.
// If they are on the same character, break, else swap.
for( j=0; j < i; j++, i--)
s[j] ^= s ^= s ^= s[j];   // swap values

}


Now, this doesn't work, mostly due to the fact that if you keep multiplying a signed integer by something > 1, it'll eventually wrap around and become negative due. I do feel that there's another, much easier way to get each digit of a decimal number from the integer, but for the life of me, I can't think what it is right now.

##### Share on other sites
Try using this (if you don't mind using a few library calls).

#include <stdlib.h>#include <iostream.h>void printDigits(int number){ char stringBuffer[255]; //Where the variable will go _itoa( number, stringBuffer, 10 );  for(int i=0; i<strlen(stringBuffer); i++) {  cout << "Digit: " << stringBuffer << endl; }}void main(){ printDigits(1940203);}//Output should be://Digit: 1//Digit: 9//Digit: 4//Digit: 0//Digit: 2//Digit: 0//Digit: 3

g/l

##### Share on other sites
he said he didnt want to use lib calls so try something like this:

int atoi(char* str)
{
int sign=1;
int x=0; /
if (NULL==str)return 0;
if ('-'==str[0]){sign=-1; x=1;}

int TheNumber=0;
int tmp;

int l=strlen(str)-1;

for(;x ='0')&&(str[x]<

##### Share on other sites

int atoi(char* str)
{
int sign=1;
int x=0;
if (NULL==str)return 0;

if ('-'==str[0]){sign=-1; x=1;}

int TheNumber=0;
int tmp;

int l=strlen(str)-1;

for(;x ='0')&&(str[x]

##### Share on other sites
bah look here, in the bottom
http://www.csharphelp.com/archives2/archive370.html

##### Share on other sites
Maybe you can try this:

if(num == 0){  *s++ = '0';  *s = '\0';  return;}if(num < 0){  num = -num;  neg = 1;}for(; num != 0; num /= 10)  *s++ = num%10;if(neg)  *s++ = '-';*s = '\0';// reverse string...

##### Share on other sites
Quote:
 Original post by EndarI'm looking to be able to get an integer and print it into a string as a decimal number. And I'm writing the function myself, because I'm messing around with writing a small OS, and I'll probably need this, before I port a C library (if ever).

About the small OS part. You can port parts of libc to your project assuming its not commercial and you don't mind releasing your source. That way you can still use native win32 code when compiling to a flat binary format for use in a simple OS.

Its pretty easy to do actually. Just goto http://www.gnu.org/software/libc/libc.html and download the latest release. Uncompress it and take the files you need into another directory. Make sure you have all the required files then compile it to a flat binary in order to link it later on.

ex for asm code: nasm -f aout myfile.asm -o myfile.bin
ex for c code: gcc myfile.c -o myfile.bin

The second should only be used with an appropriate linker file (see: www.osdever.com for more info on this. An awsome site too.

##### Share on other sites
This is probably as obfuscated as your method... and I'm not certain if it robust. Just off the top of my head.

Anyway, if speed is not a factor, any method you think of will work fine.

<source>
void sprintDec(char* s, int num)
{
int index = 0;
int offset = 10000;

if(num<0)
s[index++] = "-";

while(offset>0)
{
s[index] = (num % offset);
if(s[index] != 0)
{
index++;
}
{
index++;
}
offset /= 10;
}
}
</source>

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015462
×