Printing Integers (Hex, Decimal, etc) as Strings

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Printing Integers (Hex, Decimal, etc) as Strings

Post by t0xic »

Hey everyone,

I know this may sound like a stupid question, but how do I convert an integer to a base (hex, decimal) and print it as a string? I have tried looking at other people's code but I don't understand it, and I am trying to make my own printf() -- printinf strings and integers less than 255 and 100 ( hex, and dec ) works, but I cant print higher numbers.

Could anyone post some example code or an explanation of how to do this?

Thanks,

--T0xic
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

It could be better if you had asked this question in the General Programming board but anyway.

To convert a decimal to string, you have to divide the number by 10 each time and do that until the quotient is zero. Each time you divide, keep the remainder and then print the remainder in the reverse order as characters. For example, I have the number 43 and I want to print it as a string. I will keep dividing it by 10 and keep the remainders:

43 / 10 (Quotient = 4, remainder = 3)
Now quotient is 4 so divide the quotient by 10 again:
4 / 10 (Quotient = 0, remainder = 4)
The quotient is zero so stop right now.


Now look at the remainders in the reverse order! There you will get 4 and 3 which if printed contiguously will make 43. To covert an integral number to its string counterpart OR it with 0x30.


Now if you want to convert a base10 value to hex and print it as string, then slice the value into 4bits groups. For example a 32-bit integer can make 8 nibbles. Each nibble is a hexadecimal letter. Start with the leftmost nibble because that is the nibble that should be converted to string first. Now take the nibble and then OR it with 0x30 to make it a character. Now values between 0 to 9 will be converted correctly but you should watch for values between 10 and 15 because they should not be converted character like that. For example, if the value in the nibble is 13, and you OR it with 30, you will get 0x3D that is the character for '=' instead of 'D'. Now the trick is to add the value 7 to it to get 0x44 that is the character for 'D'.

If you are using ASM, you can also avoid doing the division-by-10 when you want to convert the decimal to string.

Good luck.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

You could look at the PDCLib code for a "complete" solution (including all the formatting required by a starndard printf()), or you could work from this tidbit of recursive code:

Code: Select all

char * digits = "0123456789abcdefghijklmnopqrstuvwxyz";

void int2base( int value, int base )
{
    if ( ( value / base ) != 0 )
    {
        int2base( value / base, base );
    }
    putchar( digits[ value % base ] );
}
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

to get past the 0x3D type thing, you just make an array of the hex digits..
like
"char hex_digits[16]={'0123456789ABCDEF'};"
actually, I'm not even sure that will compile, but you get the point..
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Thanks for you're help everyone
Post Reply