You heard me, I want to know your guys' techniques for converting data types without using additional headers (such as studio.h).

Are there any tricks or intrinsic methods? Can I write my own method? I simply don't like adding unnecessary headers becasue I don't use 95% of the crap that comes along with them, and it gets compiled with my program anyway...

Most specifically, at the moment, I want to know how to convert a float to a string. there was a command I saw, like printf or something, but I'd be darned if I understood that! There were %s everywhere and it looks like I need the studio header..

Can I type cast? atoi and atof and all that require a header.. :-|

Ones you could help me with, if you're up to the challenge:
float --> char[]
char[] --> float
char[] --> int
and any others that you know, but I'm only interested in the above at the moment.. Thankie! :cheesy:

Recommended Answers

All 8 Replies

I simply don't like adding unnecessary headers becasue I don't use 95% of the crap that comes along with them, and it gets compiled with my program anyway...

Well, uh, no, it doesn't. Is that your only reason?

Can I type cast?

If there's one thing to avoid when you're new, it's throwing casts at code.

So now, which language: C or C++?

C++ :rolleyes:
and.. what do you mean it doesn't?? :-|
even still, I like to do things the hard way meaning everything (as much possible) is my own code and not refrenced in a header :cheesy: but... I may just have to resort to the headers :cry:

oups, the kids are at the window.. time to go outside!!

C++ :rolleyes:

(Because I'd do it differently in C versus C++. :rolleyes: )
http://www.eskimo.com/~scs/C-faq/q13.1.html
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1

and.. what do you mean it doesn't?? :-|

Any half-assed library should be implemented to link only referenced code: if you don't use it, you don't pay for it.

If that's not your case, then it's a QoI issue with your toolchain vendor.

even still, I like to do things the hard way meaning everything (as much possible) is my own code and not refrenced in a header :cheesy:

Then why C++? Why not assembly? Get real dirty while reinventing the millionth wheel.

Point being: use the language for what it is for: solving a problem.

In my opinion, this is a pretty silly request - in any case, tell us the headers you've already included so we can come up with a way given the methods already provided.

Haha, I don't think any of you get what I'm asking for :rolleyes:
Here's an example, I wrote the following from scratch and used no headers (except for iostream, necessary) and it will convert an int to a string.. I don't know how fool-proof it is because it was quick, but you may understand now what I'm saying...

#include <iostream>
using namespace std;

void CPushR(char * theChars, char pushChar, int strLen);
void ItoC(char * Buffer, int theNum);

int main(){
	int iMyNum = 145627;
	char MyChar[15];
	MyChar[0]='\0';
	ItoC(MyChar, iMyNum);
	cout << MyChar << "\n";
}

void ItoC(char * Buffer, int theNum)
{
	// convert int to char[] array
	
	int Rema = 0;
	while (theNum > 0) {
		Rema = theNum % 10;
		theNum /= 10;
		CPushR(Buffer, '0' + Rema, 15);
	}
	
}

void CPushR(char * theChars, char pushChar, int strLen)
{
	// push the characters in an array right, insert new val at beginning
	// characters that fall beyond the limit of theChars are lost
	
	int Pos = 0;
	char TempChar;
	while (Pos < strLen){
		TempChar = theChars[Pos];
		theChars[Pos] = pushChar;
		//if (pushChar == '\0') break;
		pushChar = TempChar;
		Pos++;
	}
	theChars[strLen-1] = '\0'; // always terminate string at end
}

I don't want to use assembly beacuse I'm trying to learn this language, and I don't want to use unnecessary headers because I feel that I'm going to learn more and grow a lot more flexable if I figure things out on my own.. For now atleast, to make sure that I can think around problems without resorting to pre-programmed headers.. This, I consider a little lazy (though more efficient and reliable, sometimes).. So on that note, I think I'll get back to figuring out this same problem for the other data types (float, double, long, etc)..

:cheesy:

that may be fun to write, and you will most likely learn a great deal from it, but you should learn to use standard c++ or C functions, afterall that's why they are standard.
For example, here is how to convert an int into string with no effort

#include <string>
#include <sstream>

int main()
{
   int n = 12345;
   std::stringstream s;
   s << n; // convert to string
   cout << s.str() << endl; // output the string 
   
   return 0;
}

ok, so maybe you want to put it in a character array instead of displaying it

#include <string>
#include <sstream>

int main()
{
   int n = 12345;
   char array[20];
   std::stringstream s;
   s << n; // convert to string
   strcpy(array,s.str());
   cout << array << endl; // output the string 
   
   return 0;
}

Now, the above program contains 3 executable program lines. How many does yours contain, that does nothing more than what I did?

Haha, I don't think any of you get what I'm asking for :rolleyes:

Haha, I don't think you get any of what has been mentioned thus far. :rolleyes:

In general, good headers don't contain executable code. But go ahead and learn some things. Post what you think you know and folks can offer corrections (like how to protect your code from exhibiting undefined behavior by allowing access beyond array bounds). When you catch up, you'll use the standard functions to do standard functions.

Oh, and before you think you've conquered the world and need to move on, pass a negative value to your function.

:cheesy: that's what I'm here for! To ask questions and get corrected, don't worry I'm not trying to proove anything. Thanks guys!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.