Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number to text

Status
Not open for further replies.

evanmars

Programmer
Jan 20, 2003
4
0
0
US
how do I convert a number to text?
i.e the number 1000 to "1000"
 
(1) presumeably you need to output your number in decimal? If so, you need first to find a way to get individual digits out of it. The obvious way is to divide by 10 keeping the remainder (if in x86 processor, look at the div instruction). Also keep the divided number, because when you divide that by 10 again, it will give you the next digit.
(2) Unfortunately this will give you the digits in the reverse of the correct order... check it on paper. You will need to reverse the digits. Have a little think about how to do that and reply again if you get stuck.
(3) Now you need to output each digit as text. That means converting each digit to ascii. Write out a little conversion table on paper of 0 to "0" ascii etc... up to 9. It will become obvious how to make the conversion.
(4) You will have to have a think about your operating system for how to output an ascii. If in dos, have a look at the functions of int 021h.
 
Thanks, I figured it out last night.

divide the number by 10
add 48 to the remainder to convert to ascii
store result in an array
repeat above with quotient, storing result in array in descending elements


btw, I'm using masm32.
 
Absolutely! Array is nice because it's flexible about length. Some people prefer to push the digits onto the stack as they're calculated, and then pop them back off to print.
Obviously when you've divided your number down to zero then you can stop, and that way you won't even get leading zeros.

One day someone will explain to me how to print out a decimal from a binary floating point....
 
I've figured out this much on Floating-Point to Decimal:

The most significant bit (bit31 for single precision or bit 63 for double precision) is the sign. 0 is +, 1 is -.

For single precision (32 bit) the next 8 bits (bit30 - bit23) are the exponent. For double precision (64 bit) the next eleven bits (bit 62 - bit 52) are the exponent.

In single precision, a bias of 127 is added to the exponent. In double, a bias of 1023 is added.

The mantissa or significand are the remaining bits. Since the first digit of the number is always one, it is not explicity stored in the floating point format.

Subtract the bias from the exponent and convert into decimal.

If the exponent is positive, the integer portion of the number is 1 (always there though not specified) followed by the bits from the most significant bit of the mantissa ((bit 22 for single precision, bit 51 for double) to msb-exponent+1.
i.e. if the floating point number is stored as
01000011100110011001100110011001 (32 bit)
the sign is +,
the biased exponent is 10000111,
and the mantissa is 00110011001100110011001

The exponent becomes 10000111 - 01111111 = 1000 base2 or 8 base10.

The integer part of the mantissa is 100110011 (notice the 1 in the msb position followed by the first 8 digits of the mantissa).
Convert this to decimal to get 307.

For the fractional part you take the remaining bits of the mantissa and multiply that value by 2^exponent.
001100110011001 * 2^8 = 00110011001101 * 100000000 = 110011001100100000000 base2 or 1677568 base10.

The final result then is 307.1677568
 
A good explanation about converting (single precision) floating point numbers to decimal and vice versa can be found here: Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top