Fix sprintf removing leading 0's

This is a discussion on Fix sprintf removing leading 0's within the C Programming forums, part of the General Programming Boards category; I'm writing a homework assignment that requires me to convert various strings to ints and back. From the FAQ I'm ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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, 01:59 PM

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