OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 7:53 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: digit to char
PostPosted: Sat Dec 28, 2002 6:13 am 
I have a question, how do I transform a digit into a char ?


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 10:27 am 
Add 30h to the digit.


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 11:44 am 
How do I do that ?


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 12:16 pm 
[attachment deleted by admin]


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 12:39 pm 
I am writing a printk function and want to print like this
printk("Number is =%i",number).I've made a function that almost does this all i need to find a way to transform a digit(in decimal base) to a char.Lets say
char *string;
int number=3;
string[pos]=((char)number)(this is what i'm asking for, I know that this isn't working but i think this is the way i should do it) I want to transform the digit 3 into a char and write it at position pos in string.


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 1:36 pm 
That is essentially what the function I posted does. The part that converts a 'single digit' to a 'single character' is done by

[tt]xlat_table[[y]][/tt]

As was pointed out by CodeSlasher, this can also be done by an offset from the ASCII value of '0' (48, or 0x030), which would be,

[tt]y + 0x30[/tt]

The table lookup version is more general (it will work with other character encoding formats than ASCII with only some minor changes), but the addition version is faster and smaller.

These approaches can be used just like that, but they only work correctly if you know ahead of time that the value comes out to a one decimal digit (that is to say, it is a value between 0 and 9, inclusive).

The problem is, binary (base 2) values don't exactly come even to the decimal (base 10) values. Three bits will exactly represent values up to 8, which is too few; while four bits represents values up to 16, which is too many. If you try to convert a four bit value to decimal, if the value is 0x0A or higher then it would be equal to two decimal digits, not one. This obviously won't work with the conversion schemes above.

The algorithm given earlier is designed to isolate the low digits of the number one at a time, so that they can be converted correctly. Since it produces the low values first, you then need to reorder the result so that the last digit found is the first one in the string. In languages with string libraries that handle concatenation and automatically resizing, this most easily done by recursing through the value so that the result of each pass is built from the value of the pass that follows it; otherwise, you have to do some array juggling like I did in the iterative version.

Urk. I hope that that explantion won't need another explanation, but somehow I think it might... perhaps I'll need to find a clearer way to describe this.


Top
  
 
 Post subject: Re:digit to char
PostPosted: Sat Dec 28, 2002 7:38 pm 
I have a printf that handles everything:

High numbers, hex, chars, strings, etc at:

http://fritzos.sourceforge.net/

Download prekernel 0.7.1 and open up fstdio.h in the include dir.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 42 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group