Unsigned char error!

This is a discussion on Unsigned char error! within the C++ Programming forums, part of the General Programming Boards category; Hi all! I encountered a problem using Visual C++. my objective is to convert "char tmp" to a decimal number, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    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;
    sprintf(buf,"%d",tmp);
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21