Thread: How to make itoa write the left zeros?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    How to make itoa write the left zeros?

    Hello,
    I' trying to convert hexadecimal to binary, I'm using itoa and it seems to be working right, the problem is that it skip every zero at the left of the first one. Is there any way to force it to write this zeros?
    Code:
    printf("%s ", itoa(str2 , str, 2) );
    if str2="f" everything is Ok, it prints 1111
    if str2="2" it prints only 10, I would like to print 0010

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    check the strlen() of received string and do the loop:
    for (i = 1; i <= 4 - strlen(itoa(...))); i++)
    printf("0");

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think that the most efficient option in this case would be to not use itoa. It's not designed for what you want. Just declare a mask variable, make a loop that loops until the mask is zero, shifting it right each time, then test each bit of the input by anding it with the mask.
    Code:
    char buffer[16], len= 0;
    for (int mask = 0x8; mask > 0; mask >>= 1)
    {
        buffer[len++] = (input & mask) ? '1' : '0';
    }
    buffer[len++] = '\0';
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    better to make mask unsigned int
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    sorry to both you, but i just joined the fourm and i am wondering how do i start a new post because i have a question with my code? thanks

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Press new thread where the rest questions are. Search and you will see it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. How can we write a C shell script to make Dirs
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 04-27-2002, 10:27 PM
  5. write then make R-O
    By confusedcoder in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2002, 07:53 AM

Tags for this Thread