Convert unsigned long to irr::core::stringw

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Convert unsigned long to irr::core::stringw

Post by randomMesh »

I noticed, that there is no overloaded constructor for an unsigned long in the class string in irrString.h.

so

Code: Select all

const unsigned long ul = ULONG_MAX;
irr::core::stringw(ul);
would give a compiler error.

So i wrote this function to do the conversion:

Code: Select all

irr::core::stringw ulTOstringw(const unsigned long number)
{
#if ULONG_MAX == 0xffffffff
	char buff[11]; //10 chars of ULONG_MAX + '\0' if unsigned long is 32bit
#elif
	char buff[21]; //20 chars of ULONG_MAX + '\0' if unsigned long is 64bit
#endif
	snprintf(buff, sizeof(buff), "%lu", number);

	return irr::core::stringw(buff);
}
Is this the way to go or is there a better way to do so?
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Post by torleif »

Longs have different meanings of different systems. Avoid it.

If your after converting strings there are different ways of doing it.

I found that it's best to work with arrays in irrlicht to convert a type of string to another, then build up the array at the end of the function to a string.

Another is simply to rip through the pointers and append it to the end str:

example:

Code: Select all

irr::core::stringw ulTOstringw(u32 * mchar)
{
   core::stringw ret; 
   
   while (char != 0) {
       ret.append((u16)mchar++)
   }
   return ret;
} 
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

There is nothing really wrong with the original code, though it might be a good idea to avoid conditional compilation and just make the buffer big enough for a stringified 64-bit unsigned. It might also be useful to use the C99 function swprintf() instead of snprintf.
torleif wrote:Another is simply to rip through the pointers and append it to the end str:

example:

Code: Select all

irr::core::stringw ulTOstringw(u32 * mchar)
{
   core::stringw ret; 
   
   while (char != 0) {
       ret.append((u16)mchar++)
   }
   return ret;
} 
Uh, yeah. that will not do what you, or the original poster, want. First off, it would never compile, and if you fixed that, it would loop until you ran out of memory, an access violation, or mchar wrapped around to 0. IMO, the original posters code is much better.

If you really wanted to write your own, you would typically mod by 10 and convert the result from an integer 0-9 to a character '0'-'9', then divide the remainder by 10. Append the character, and repeat until the remainder is 0. Once you have the resulting string, you have to reverse it. There are other ways, but this is the most obvious.

Travis
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

torleif wrote:Longs have different meanings of different systems. Avoid it.
right, if you use an int it should work...

and this is not realy correct, at least it does nothing usefull:

Code: Select all

irr::core::stringw(ul);
you don't create a variable to hold the string... ;)
this is better:

Code: Select all

irr::core::stringw myString(ul);
so this should work as expected:

Code: Select all

const unsigned int ul = ULONG_MAX;
irr::core::stringw myString(ul);
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Acki wrote:right, if you use an int it should work...
I don't want an int, i want an unsigned long.
Acki wrote:and this is not realy correct, at least it does nothing usefull:

Code: Select all

irr::core::stringw(ul);
you don't create a variable to hold the string... ;)
Actually, i want to do this:

Code: Select all

case SVT_UNSIGNED_LONG:
	valueString += irr::core::stringw(var.UnsignedLongVal);
	addressString += makeStringFromAddress((unsigned long)&var.UnsignedLongVal);
	break;
This is useful. ;)
Acki wrote:this is better:

Code: Select all

irr::core::stringw myString(ul);
I want anonymous objects.

So i changed the code to

Code: Select all

irr::core::stringw ulTOstringw(const unsigned long number)
{
	wchar_t buff[21]; //20 wchar_t of ULONG_MAX + '\0' if 64bit
	swprintf(buff, sizeof(buff), L"%lu", number);

	return irr::core::stringw(buff);
}
and can write

Code: Select all

valueString += ulTOstringw(var.UnsignedLongVal);
Thanks to all.
Post Reply