Numbers to string

Programming, for all ages and all languages.
Post Reply
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Numbers to string

Post by teodori »

Hello, how do I convert a number to a string. I want a function like printf() or itoa().
User avatar
Combuster
Member
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

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: Numbers to string

Post 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";
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Numbers to string

Post 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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
siavoshkc
Member
Member
Posts: 40
Joined: Wed Feb 19, 2014 11:10 am

Re: Numbers to string

Post 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.
Check out my FSB Data Integrity Tester at http://fsbdit.sourceforge.net/.

Siavosh
AbstractYouShudNow
Member
Member
Posts: 92
Joined: Tue Aug 14, 2012 8:51 am

Re: Numbers to string

Post 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.
User avatar
Pancakes
Member
Member
Posts: 75
Joined: Mon Mar 19, 2012 1:52 pm

Re: Numbers to string

Post 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
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

Re: Numbers to string

Post 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));
}
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: Numbers to string

Post 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.
Those who understand Unix are doomed to copy it, poorly.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Numbers to string

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply