Thread: Unsigned char error!

  1. #1
    Registered User
    Join Date
    Jan 2006

    Smile Unsigned char error!

    Hi all!

    I encountered a problem using Visual C++. my objective is to convert "char tmp" to a decimal number, but bcos char supports only -128 to 128 and i want it to be from 0 to 256, so i changed it to unsigned char instead. However, an error occurs when i use "unsigned char" with "sprintf". My code is as follows:

    unsigned char tmp='a',buf;
    int num;
    num = atoi(buf);

    Errors occurs when the above code is used:

    "error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned char' to 'char *'

    Is there other alternative methods to implement the codes above since i can't use "unsigned char" with "sprintf"?

    Thanks a million!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    sprintf prints to an array, not a char.
    Likewise, atoi reads from an array.

    So do
    char buf[20];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM