Thread: A Little help on conversions.....

  1. #1
    SublicK
    Guest

    A Little help on conversions.....

    OK..... I've asked this before, but I think the wording was off a bit.

    I want to convert a INT var into a CHAR var. NOT A STRING!!

    this is a way...but it converts to a string:
    sprintf(string, "%i", integer)

    can I do this:

    Code:
    int IntVar = 99;
    char CharVar[20];
    
    sprintf(CharVar, "%i",IntVar);
    I'm new at C++ (used to program in QBaisc)
    I must be stupid or something :P

    anyway, any help would be cool.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <iostream.h>
    #include <stdio.h>
    int main(void) {
        int IntVar = 99;
        printf( "%d %c\n", IntVar, IntVar );   // prints 99 c
        cout << IntVar << " " << (char)IntVar << endl;   // ditto
        char ch = (char)IntVar;
        printf( "%d %c\n", ch, ch );   // prints 99 c
        cout << (int)ch << " " << ch << endl;   // ditto
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SublicK
    Guest

    Char vrs Unsigned

    what is the difference between 'char' and 'unsigned char'

    I am using Allegro and the textout function needs a unsigned char....
    how can I use 'char CharBuffer = (char)IntVar'
    to make a unsigned char istead of a char?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void textout(BITMAP *bmp, FONT *f, unsigned char *str, int x, int y, int color);

    unsigned char cbuff[2];
    cbuff[0] = IntVar;
    cbuff[1] = '\0';
    textout( bmap, font, cbuff, x, y, col );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arithmetic conversions
    By wilksdr in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 09:10 PM
  2. why are enum type conversions disallowed?
    By krygen in forum C++ Programming
    Replies: 11
    Last Post: 11-24-2004, 11:22 AM
  3. type conversions
    By kocika73 in forum C Programming
    Replies: 2
    Last Post: 10-06-2004, 09:45 AM
  4. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 09:23 AM
  5. Conversions
    By Bones in forum C++ Programming
    Replies: 4
    Last Post: 09-01-2003, 09:59 AM