![]() |
|
#1
|
|||
|
|||
problems typecasting an int to a stringHello,
Just a quick problem, I can't figure out how to cast an int to a string. I don't want it to read "Forty Five" for instance. just to put the digits in the string.... so 45 becomes "45" Thanks in advance |
|||
|
#2
|
||||
|
||||
|
do you mean to cast an int to string. then sprintf would do the job.
sprintf(YourString, "%d",YourInt); __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#3
|
||||
|
||||
|
there's also an itoa function in addition to itoa.
__________________
-Aaron |
|
#4
|
||||
|
||||
|
Quote:
itoa is not a standard function. I think Borland compilers always include it, but most don't. I have just lately become a big fan of sprintf and I think it works better than itoa in most cases anyway. Cheers, d |
|
#5
|
||||
|
||||
|
sprintf kicks butt for sure!
__________________
-Aaron |
|
#6
|
|||
|
|||
|
Hi, i am pasting this following code just for the heck of it! I've ported this function from assembly to c, so whomever of you want to get an idea of how converting from integer to characters actually works, you can learn from it! But you must know how bitwise operations work. So if you don't, i suggest you look them up.
I can't take any credit for this function or concept since it's been used in assembly forever. I've just translated it into c, from my memory, and i should stress that it may not be the most efficient way, but it's plenty efficent. Also this function only works with hex. I could have written 2 more functions which convert hex to decimal and vice versa, but i'll do it later I've commented the hell out of this code, because it took me a little while to get the concept. Eitherway i'll summarize the concept here: When we have an integer stored, say in a byte, it will have 8 bits in its binary form. Now when we convert that to hexadecimal, it will have only 2 digits instead of 8 in binary because binary is base 2, and hex is base 16 so 16/2 = 8. so lets take a look at an integer say 24H (24 hex = 36 decimal) which will be represented as 00100100 in binary. Each 4 bits will represent a single hex digit. The 4 high bits represent 32 decimal or 20 hex, and 4 low bits represent 4 decimal or 4 hex. Ok so what do we do when we wanna convert from an integer form to a character form? how do we single out each hex digit? As we know that hex digits run from 0 to f (which is 15 decimal), we create a "lookup" table which is very ingenious! It is a table of character strings which contains values from 0 thru 15 or f(hex), here: char *lookup = "0123456789abcdef" This contains all the different hex digits and the ingenious part is that they are all placed on their appropriate index! lookup[13] will give you the character 'd'. so if you had a binary digit say 00001010 which is 10 decimal or A hex and if you used the integer to reference the lookup for example, short A = 0xa; lookup[A]; which will give you the character 'a'. there you have your character! Now this only works for single hex digits, so a single hex digit can at max contain the lowest 4 ON bits or 4 righmost ON bits. for example 00001111 = 0f in hex which is 15 decimal, after f, hex becomes 2 digits or 10H(10 hex = 16 decimal) or just like after 9, decimal becomes 2 digits or 10. So each time we use the lookup, we have to make sure that we do it hex digit by hex digit, or 4 lowest bits by 4 lowest bits. Below is the code, im sure it will make sure after you go thru it a bunch of times. This post might not be suitable for the help forum, if that is the case, im sorry, dsmith may remove it or post it in tutorials section. I would've done it so, but its just one function! CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
|
Here is an elegant solution.
I hard coded it for integers, but it can and should really be templated so it would work on any type. -Ken CPP / C++ / C Code:
Quote:
|
|
#8
|
|||
|
|||
|
Thanks alot everybody, I havn't had time to go through your post Machinated, but thank you in particular to kzutter, I will be using that function for sure!
|
|
#9
|
|||
|
|||
|
dont worry about my post, it doesn't even do waht i wanted it to do, and i don't think it's possible to convert a decimal number to a decimal array my way. My apologies, this is what happens when you just start writing code without planning anything! I've turned that function into a decimal to hexadecimal converter, so i guess it's good for something
![]() |
|
#10
|
|||
|
|||
|
Glad it was good for something Machinated.
Oh and I'm using your Int2String Function as a Method in my C++ project kzutter, hope you don't mind. |
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Including Maps and strings?? | maddie | C++ Forum | 17 | 05-Jul-2004 06:25 |
| string compare | sfeng12 | C++ Forum | 2 | 03-May-2004 22:27 |
| some I/O problems...again | cameron | C++ Forum | 3 | 03-Mar-2004 21:39 |
| [function] AutoLink (converts URLS into links inside a string) | JdS | PHP Code Library | 0 | 26-Jan-2004 05:02 |
| storing a token pointer as a string | CoreLEx | C Programming Language | 1 | 07-Oct-2003 11:33 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The