Jump to content
  • Advertisement
Sign in to follow this  
Endar

Getting every digit of a decimal number

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
bah look here, in the bottom
http://www.csharphelp.com/archives2/archive370.html

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Endar
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).


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.

You may also want to consider dynamically linking it to your code at runtime so you can use it without GPL'ing your OS code. (Pretty sure about this)

Share this post


Link to post
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 leadingZero = 1;
int offset = 10000;

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!