Thread: Fix sprintf removing leading 0's

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    Fix sprintf removing leading 0's

    I'm writing a homework assignment that requires me to convert various strings to ints and back. From the FAQ I'm using sprint and snprintf(as we're supposed to show various ways of doing this) to convert an int to a string. Everything works great until I enter a "binary" number in the form of int i = 0011, which sprintf and snprintf convert to char[] = 11.

    I've racked my brain and searched the forums for how to get around the automatic removal of the leading 0's to no avail, so any suggestions would be greatly appreciated.

    Thanks.

  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
    You need to read the input as a string if the number of leading zeros is important to you.
    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
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Even I had a question about. But i was pretty sure you cant do it unless u state it as a string. But now i am clear.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Removing leading and trailing whitespace
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 03:55 PM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM