Numbers to string
Numbers to string
Hello, how do I convert a number to a string. I want a function like printf() or itoa().
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Numbers to string
The noob virus is worse than ever this season - it even infected 2-star people
Surely you know how to convert binary to decimal by hand, no?
Surely you know how to convert binary to decimal by hand, no?
Re: Numbers to string
Sorry I don't understand that... I know that 16 is 0x10 and 0b1000, but do I transform:
Code: Select all
int i = 8192;
to
char s[] = "8192";
- thepowersgang
- Member
- Posts: 734
- Joined: Tue Dec 25, 2007 6:03 am
- Libera.chat IRC: thePowersGang
- Location: Perth, Western Australia
- Contact:
Re: Numbers to string
Grab a piece of paper, and figure out how you can use the operations "divide" and "modulus" to break a large number up into many smaller numbers ready for printing. (If you're really stuck... go searching for implementations of itoa... but if you're having troubles even getting started with this concept, then you're better off doing more applications programming before attempting systems)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Re: Numbers to string
You mean you want to assembly code of itoa()? You can use a debugger to see it. Just put a breakpoint at the function call in C and then step into it.teodori wrote:Hello, how do I convert a number to a string. I want a function like printf() or itoa().
-
- Member
- Posts: 92
- Joined: Tue Aug 14, 2012 8:51 am
Re: Numbers to string
Is google so difficult to use ?
If you use too much code from other people in your OS, well... it won't really be YOUR OS.
If you use too much code from other people in your OS, well... it won't really be YOUR OS.
Re: Numbers to string
Well, ..this is general programming so here you go. It produces hex output only. Doing base 10 is a little different, but not by much.
http://wiki.osdev.org/User:Pancakes
http://wiki.osdev.org/User:Pancakes
Re: Numbers to string
Hello, after searching a bit on google I found and modified some code. Here are my functions:
Code: Select all
void string_reverse(uint8_t* string, uint64_t length){
uint8_t ch;
uint64_t i, j;
for(i = 0, j = (length - 1); i < j; i++, j--){
ch = string[i];
string[i] = string[j];
string[j] = ch;
}
}
void string_from_uint64_dec(uint64_t n, uint8_t* string){
uint8_t i = 0;
do{
string[i++] = n % 10 + '0';
}while( (n /= 10) > 0);
string[i] = '\0';
string_reverse(string, string_length(string));
}
void string_from_int64_dec(int64_t n, uint8_t* string){
uint8_t i = 0, sign;
if(n < 0){
sign = 1;
n = -n;
}else
sign = 0;
do{
string[i++] = n % 10 + '0';
}while( (n /= 10) > 0);
if(sign)
string[i++] = '-';
string[i] = '\0';
string_reverse(string, string_length(string));
}
void string_from_uint64_hex(uint64_t n, uint8_t* string){
uint8_t i = 0, c;
do{
c = n % 0x10;
if(c > 9)
string[i++] = c + '0' + 7;
else
string[i++] = c + '0';
}while( (n /= 0x10) > 0);
string[i++] = 'x';
string[i++] = '0';
string[i] = '\0';
string_reverse(string, string_length(string));
}
void string_from_int64_hex(int64_t n, uint8_t* string){
uint8_t i = 0, c, sign;
if(n < 0){
sign = 1;
n = -n;
}else
sign = 0;
do{
c = n % 0x10;
if(c > 9)
string[i++] = c + '0' + 7;
else
string[i++] = c + '0';
}while( (n /= 0x10) > 0);
string[i++] = 'x';
string[i++] = '0';
if(sign)
string[i++] = '-';
string[i] = '\0';
string_reverse(string, string_length(string));
}
Re: Numbers to string
At first glance, those look like they should work, but do you plan to write another pair of functions when you want to convert to octal? Then again for binary?teodori wrote:Hello, after searching a bit on google I found and modified some code. Here are my functions:
A solution involving just one function to convert an unsigned integer to a string in base n, and smaller functions using it to do that part of the task, while they just check and adjust for sign and decorate the output as needed, might be easier to maintain / extend, while also being more elegant.
Those who understand Unix are doomed to copy it, poorly.
- thepowersgang
- Member
- Posts: 734
- Joined: Tue Dec 25, 2007 6:03 am
- Libera.chat IRC: thePowersGang
- Location: Perth, Western Australia
- Contact:
Re: Numbers to string
That code... props for searching google, but that code is just wrong. All of that can be implemented in a single method quite easily. And there are issues in that code.
Major one - the hex output is VERY hard to understand (c + '0' + 7 instead of 'A' + c - 10), when I first looked I thought it was plain wrong.
Major one - the hex output is VERY hard to understand (c + '0' + 7 instead of 'A' + c - 10), when I first looked I thought it was plain wrong.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc