Thread: int to char

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    int to char

    Is it possible to convert an int to a char?

    I have a source code where that is done but it makes no sense to me. I can see how 2 or 9 could be converted to a char, but not how 345 can be.

    Code:
    unsigned char *buffer[100];
    int value = 4333;
    
    *buffer[0] = value;
    I try to do the same in my code, it compiles, but I get a windows error when I run the program.

    So, what happens here really?

    thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by h3ro View Post
    Is it possible to convert an int to a char?

    I have a source code where that is done but it makes no sense to me. I can see how 2 or 9 could be converted to a char, but not how 345 can be.
    It can't. The compiler bit-slices the larger value to fit into the smaller one. It loses data.

    Code:
    unsigned char *buffer[100];
    int value = 4333;
    
    *buffer[0] = value;
    I try to do the same in my code, it compiles, but I get a windows error when I run the program.
    I doubt that very much. EDIT: the problem is that buffer is an array of 100 pointers to char. This array is uninitialized. You take the first pointer (buffer[0]) and dereference it. This causes a crash. The problem has nothing to do with the conversion.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Have you heard of a static_cast?

    Code:
     int num; // user input
       
       std::cout << "Enter a number: ";
       std::cin >> num;
       
       // output the character using a static cast
       std::cout << "\ncharacter equivalent: " << static_cast<char>( num );
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    unsigned char *buffer[100];
    int value = 4333;
    
    *buffer[0] = value;
    this allocates an array of 100 pointers to unsigned chars. But doeasnt allocate any space for the chars . buffer[0] points to a random location. dereferencing it -> boom.

    BTW to assigne an int to a char no cast is needed.

    Kurt

    EDIT: just noticed that brewbuck has alredy mentioned that. sorry

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by swgh View Post
    Have you heard of a static_cast?
    Did you actually try it to make sure it did anything useful?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I'm sorry, but I dont really inderstand what the problem is. I am still having truble with pointers and are not sure on how they work.

    Code:
    unsigned char  *colorBuffer[300 * 400 * 3];
    unsigned int r = 0, g = 0, b = 0;
    
     // GetR,G,B returns an int between 0 and 255
     r = currentMaterial.getR();
     g = currentMaterial.getG();
     b = currentMaterial.getB();
                                            
     *colorBuffer[arrayTracer + 0] = r;
     *colorBuffer[arrayTracer + 1] = g;
     *colorBuffer[arrayTracer + 2] = b;
    Its the *colorBuffers that couse the problem. I feel kind of clueless right now.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would think that you don't want an arry of pointers to char but an arry of chars
    like this
    Code:
    unsigned char  colorBuffer[300 * 400 * 3];
    unsigned int r = 0, g = 0, b = 0;
    
     // GetR,G,B returns an int between 0 and 255
     r = currentMaterial.getR();
     g = currentMaterial.getG();
     b = currentMaterial.getB();
                                            
     colorBuffer[arrayTracer + 0] = r;
     colorBuffer[arrayTracer + 1] = g;
     colorBuffer[arrayTracer + 2] = b;
    Kurt

    edit: If you really want it to be an arry of pointers to char you would have to allocate space for the chars but that is most unusual ( and a wast of space ).
    like this
    Code:
    unsigned char * colorBuffer[300 * 400 * 3];
    unsigned int r = 0, g = 0, b = 0;
    int i;
    for ( i = 0; i < 300 * 400 * 3; ++i )
        colorBuffer[i] = malloc(1);
    
     // GetR,G,B returns an int between 0 and 255
     r = currentMaterial.getR();
     g = currentMaterial.getG();
     b = currentMaterial.getB();
                                            
    * colorBuffer[arrayTracer + 0] = r;
    * colorBuffer[arrayTracer + 1] = g;
    * colorBuffer[arrayTracer + 2] = b;
    Last edited by ZuK; 04-17-2007 at 01:22 PM.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Think of pointers as the 'Shortcuts' you can create in Windows to files - only C++ pointers point to units of memory. You create a pointer like this: type it will point to* It's name.
    Then, to extract the information it points to, you write *it's name. That * can mean two things! So what is happening here:
    Code:
    *colorBuffer[arrayTracer + 0] = r;
    is that someone is using pointer called colourbuffer, and extracting (dereferencing) the information from the thing it points to (a variable, most likely). Hope it explained it a bit, I'm not much of an explainer.

    EDIT: I'm assuming that you didn't write this code yourself? Because you sounded unsure.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thank you

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