Convert int to char

Is there a better way of converting an int into a char than this…?

int myNum = 9;  
string myNumString = ofToString( myNum );  
const char * myNumChars = myNumString.c_str();  
char myChar = myNumChars[0];  


Well, I was gonna suggest using a stringstream… but that’s what ofToString does anyway.

Could check out the itoa function, but it’s not standard ANSI-C (ie. it’s compiler-dependent). But also on that page, are a couple notes about using sprintf to do the conversion.

I would say that sprintf is the next best method to the code you’ve already got. The disadvantage (or advantage) is that it writes directly to a char* buffer … which means you need to have that buffer allocated before your call, and it must have enough room for the entire string (probably not a huge deal for integers, because they can only contain so many digits).

In short, the method you are using is likely the best approach… I just thought I’d discuss other options so you know about them :stuck_out_tongue:

Hey Chris, what is it exactly you want to do? If it is convert a single digit integer number to char you can do this:

	int myNum = 9;  
	char myChar = '0' + myNum;  

I am trying to dynamically name some filenames in a for loop, but am getting this error:
‘expression must have integral or unscoped enum type’

for (int i = 0; i < 5; i++){
//other stuff is in here too, but this is the line producing the error
tempModel.loadModel('models/" + i + ".dae", false)

you can’t concatenate a string with an int but you can convert the integer (i) into a string using ofToString(…) so this would work (note the double quotes not single quote):

"models/" + ofToString(i) + ".dae", false
1 Like

ofToString did it!

Ok, I am permanently switching from Processing to OF:)

Thank you all for your help, this is a lifesaver

Just in case if anyone else is looking for answer, here is the code:

ofSaveImage(pix, ofToString(counter)+".png", OF_IMAGE_QUALITY_BEST);
counter++; //should be declared as zero in .h file

sorry - I am back!

Is there any way to connect a shader to ofPixels? The images are now saved with transparent backgrounds. I want to save the shader effects (not the originally inputter image) . is it possible?

Right now the dna.png image saves many times, instead of the effects:

void ofApp::setup(){
shader.load( “shaderVert.c”, “shaderFrag.c” );
counter = 0;
image.loadImage( “dna.png” );
fbo.allocate(image.getWidth(), image.getHeight(), GL_RGBA);

void ofApp::draw(){
//1. Drawing into fbo buffer
fbo.begin(); //Start drawing into buffer

ofClear(255, 255, 255, 0);
image.draw( 0,0);

fbo.end();			//End drawing into buffer

//2. Drawing to screen through the shader
shader.begin();		//Enable the shader

float time = ofGetElapsedTimef();
shader.setUniform1f( "time", time );	//Passing float parameter "time" to shader

//Draw fbo image
fbo.draw( 0, 0 );

ofPixels pix;
ofSaveImage(pix, ofToString(counter) + ".png", OF_IMAGE_QUALITY_BEST);

shader.end();		//Disable the shader


First of all I feel a little bit confused because it’s a reply for another post …so maybe move it to right place…but my answer is…you need something like this:

clear fbo

now you can draw fbo
and save it to png file…

1 Like

thank you so much. this worked. OMG this is a totally different post - this is what coding does to me :(((

1 Like