Thread: Int to Char

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Int to Char

    I am aware that OpenGL does not directly support the displaying of text, and I found this funtion, but this isnt OpenGL specific, so I thought I would post this here

    Code:
    DrawText(GLint x, GLint y, char* s, GLfloat r, GLfloat g, GLfloat b)
    {
        int lines;
        char* p;
    
    	glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glOrtho(0.0, glutGet(GLUT_WINDOW_WIDTH), 
                 0.0, glutGet(GLUT_WINDOW_HEIGHT), -1.0, 1.0);
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
        glColor3f(r,g,b);
        glRasterPos2i(x, y);
    
          for(p = s, lines = 0; *p; p++) 
    	  {
             if (*p == '\n') 
    		 {
    				lines++;
    				glRasterPos2i(x, y-(lines*18));
    		}
    			glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *p);
          }
    
    	glPopMatrix();
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
    }
    My problem is that I need to print an int, and itoa is not an ANSI standard. How else would I be able to print an int using the funtion above?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Can you manage to actually print text? If so, why don't you just use sprintf to make yourself a buffer with your text formatted all nice and purdy, and display that?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Yes I am able to ues the above funtion to print text.

    But I would like to convert an int to a char prior to the call, so I can print the integer as a char.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so what's wrong with sprintf as I suggested?

    Wait a second "print an integer as a char"? What exactly do you mean:
    Code:
    int x = 10;
    char c;
    
    c = x; /*(decimal ten)*/
    
    putchar( c );
    Or do you mean for it to actually display the string "10"? If so, use sprintf, as stated.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Sprintf returns the number of characters it print if successful.

    Im not understanding how I would use the sprintf call to convert the int to a char...

    Im going to be using the function to display an integer, but I cant use the function to display anything but a char. So I would like to convert the int to a char, and then use variable in the call to the print funtion.

    >>Or do you mean for it to actually display the string "10"? If so, use sprintf, as stated

    Yes
    Last edited by MethodMan; 11-21-2004 at 05:07 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    1# you should initialize *p with something.
    2# glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, some_int +'0' ); ??

    EDIT:
    Quote Originally Posted by MethodMan
    Sprintf returns the number of characters it print if successful.
    Code:
    int some_int = 1234;
    char p[32];
    sprintf(p,"%d",some_int);
    Display the number char by char.
    Last edited by xErath; 11-21-2004 at 05:08 PM.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Sprintf returns the number of characters it print if successful.
    It prints whatever you said to print, except it prints it into a string. Not a char by the way, but a char[].
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MethodMan
    Sprintf returns the number of characters it print if successful.

    Im not understanding how I would use the sprintf call to convert the int to a char...
    *pounds head on desk*

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This is driving me insane! I can't get a right string!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    	char str[100];
    	double num;
    	
    	scanf("%d", &num);		// Put input into variable num
    	sprintf(str, "%d", 34);
    	printf("%d --> %s\n", num, str);
    	
    	return 0;
    }
    This is how the program runs:
    Code:
    34
    34 --> q\uffff\uffff\uffff
    grrahh!

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Why can't you just conver the int to a char? You can change most of it for an int then just typecast it for the glutBitmapCharacter funciton

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    No no linuxdude, don't help them, the author of this problem was 1 month ago. Ya guys gotta focus on my problem! jeez lol.

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    *slaps Kleid-0 in the back of the head*

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you highjacking an old thread instead of starting a new one?

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Quote Originally Posted by MethodMan
    Yes I am able to ues the above funtion to print text.

    But I would like to convert an int to a char prior to the call, so I can print the integer as a char.
    A char ? I guess you meant a string ? As said before, sprintf() is your friend. Then, pass the address of the string to the function via a char * parameter.
    Emmanuel Delahaye

    "C is a sharp tool"

  15. #15
    Quote Originally Posted by MethodMan
    Sprintf returns the number of characters it print if successful.
    So what ? The main purpose of sprintf() is to populate an array of char with a string.
    This string is constructed with a printf-like format parameters and variadic parameters.
    Code:
       int x = 123;
       char s[10];
    
       sprintf (s, "%d", x);
    makes "123" in s.

    Im not understanding how I would use the sprintf call to convert the int to a char...
    This string is constructed with a printf-like format and variadic parameters.
    Code:
       int x = 123;
       char s[10];
    
       sprintf (s, "%d", x);
    makes "123" in s.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM