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
Printing Integers (Hex, Decimal, etc) as Strings
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.
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.
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.