Page 1 of 1

Numbers to string

Posted: Wed Feb 19, 2014 11:17 pm
by teodori
Hello, how do I convert a number to a string. I want a function like printf() or itoa().

Re: Numbers to string

Posted: Thu Feb 20, 2014 12:05 am
by Combuster
The noob virus is worse than ever this season - it even infected 2-star people :shock:

Surely you know how to convert binary to decimal by hand, no?

Re: Numbers to string

Posted: Thu Feb 20, 2014 12:38 am
by teodori
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";

Re: Numbers to string

Posted: Thu Feb 20, 2014 12:56 am
by thepowersgang
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)

Re: Numbers to string

Posted: Thu Feb 20, 2014 1:03 pm
by siavoshkc
teodori wrote:Hello, how do I convert a number to a string. I want a function like printf() or itoa().
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.

Re: Numbers to string

Posted: Thu Feb 20, 2014 2:12 pm
by AbstractYouShudNow
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.

Re: Numbers to string

Posted: Thu Feb 20, 2014 9:25 pm
by Pancakes
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

Re: Numbers to string

Posted: Thu Feb 20, 2014 11:36 pm
by teodori
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

Posted: Fri Feb 21, 2014 12:20 am
by Minoto
teodori wrote:Hello, after searching a bit on google I found and modified some code. Here are my functions:
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?

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.

Re: Numbers to string

Posted: Fri Feb 21, 2014 6:18 am
by thepowersgang
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.