GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2004, 17:07
holbrook holbrook is offline
New Member
 
Join Date: May 2004
Posts: 9
holbrook is on a distinguished road
Unhappy

problems typecasting an int to a string


Hello,

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  
Old 05-May-2004, 19:27
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
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  
Old 05-May-2004, 20:46
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
there's also an itoa function in addition to itoa.
__________________
-Aaron
  #4  
Old 06-May-2004, 07:48
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by aaroncohn
there's also an itoa function in addition to itoa.

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  
Old 06-May-2004, 11:55
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
sprintf kicks butt for sure!
__________________
-Aaron
  #6  
Old 06-May-2004, 13:40
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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:
#include<iostream>
using namespace std;


//converts from integer hex to char and returns a 4 char array which has 4 digits

char *int_char(short __2byte)
{
	char *_4digit;			//char array to store the result and return
	_4digit = new char[5];	//assign 5 spaces, 4 for digits and 5th for null char
	char *lookup = "0123456789abcdef";	//lookup table stores the hex digits into their
										//corresponding index. ingenious!

	char mask = 0x000f;					//mask will clear out(turn to 0) all the bits except lowest 4
										//which represent a single hex digit

	short temp = __2byte;				//temporary integer placeholder
	temp &= mask;						//mask the integer so it will only have lowest 4 bits

	_4digit[3]=lookup[temp];			//temp now contains a numeric value which will point
										//to the corresponding index in the look up table so
										//all we ahve to do is use it as an index to the lookup
										//array and voila!!! did i mention it's ingenious?
										//now that we have the lowest 4 bits, which makes up
										//the rightmost or the 4th hex digit, we store it in 
										//the 4th slot in our char array

	temp = __2byte;						//assign our entire integer to temp again!
	temp >>= 4;							//this time we are shifting 4 bits to the right
										//so that it will push off the lowest 4 bits and the
										//4 bits to the right of the lowest 4 bits will take their
										//place, since we are already done converting the original
										//lowest 4 bits (single rightmost hex digit)
	temp &= mask;						//now again we mask the integer so that it only contains
										//the lowest 4 bits!
	_4digit[2]=lookup[temp];			//temp will again be an index to the look up array and
										//fetch us our hex digit and this time we assign it to
										//the 3rd slot in our array(counting from left).
										//so now our array has the rightmost 2 hex digits
										//we still need to do 2 more, so we repeat the code!
	temp = __2byte;						//assign the entire integer to temp again
	temp >>= 8;							//this time shift 8 bits! so we will push off lowest
										//8 bits and the 4 bits to the right will take their place
	temp &= mask;						//mask it again
	_4digit[1]=lookup[temp];			//look it up and assign it to the 2nd slot in our array
										//we now have 3 digits done, now just one more!
	temp = __2byte;						//assignment
	temp >>= 12;						//shift 12 bits this time, so the 4 leftmost or highest
										//get pushed off in the place of 4 rightmost or lowest
										//bits
	temp &= mask;						//mask it again
	_4digit[0]=lookup[temp];			//fetch the digit and assign it in the 1st slot!
	
	_4digit[4]='\0';					//assign null to the last char to symbolize the end of string
	return _4digit;						//return our char array!
}	

int main()
{
	short _2byte = 0x4fc2;
	cout<<int_char(_2byte);

	return 0;
}
  #7  
Old 06-May-2004, 17:09
kzutter kzutter is offline
New Member
 
Join Date: May 2004
Posts: 3
kzutter will become famous soon enough
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:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string int2string(const int i)
{
  ostringstream stream;
  stream << i;
  return stream.str();
}

int main(int argc, char *argv[])
{
  int foo = 123;
  string bar = int2string(foo);
  cout << bar << endl;
}

Quote:
Originally Posted by holbrook
Hello,

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
  #8  
Old 06-May-2004, 18:08
holbrook holbrook is offline
New Member
 
Join Date: May 2004
Posts: 9
holbrook is on a distinguished road
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  
Old 07-May-2004, 17:38
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
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  
Old 07-May-2004, 18:59
holbrook holbrook is offline
New Member
 
Join Date: May 2004
Posts: 9
holbrook is on a distinguished road
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 GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.