CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS LANGUAGES C DETAILS
    Question :
    How can I convert integers to binary or hexadecimal?
    Category C Interview Questions
    Rating (0.3) By 9 users
    Added on 11/17/2011
    Views 151
    Rate it!
    Answers:

    Make sure you really know what you're asking. Integers are stored internally in binary, although for most purposes it is not incorrect to think of them as being in octal, decimal, or hexadecimal, whichever is convenient. The base in which a number is expressed matters only when that number is read in from or written out to the outside world, either in the form of a source code constant or in the form of I/O performed by a program.
    In source code, a non-decimal base is indicated by a leading 0 or 0x (for octal or hexadecimal, respectively). During I/O, the base of a formatted number is controlled in the printf and scanf family of functions by the choice of format specifier (%d, %o, %x, etc.) and in the strtol and strtoul functions by the third argument. During binary I/O, however, the base again becomes immaterial: if numbers are being read or written as individual bytes (typically with getc or putc), or as multi-byte words (typically with fread or fwrite), it is meaningless to ask what ``base'' they are in.
    If what you need is formatted binary conversion, it's easy enough to do. Here is a little function for formatting a number in a requested base:

    char *
    baseconv(unsigned int num, int base)
    {
    static char retbuf[33];
    char *p;

    if(base < 2 || base > 16)
    return NULL;

    p = &retbuf[sizeof(retbuf)-1];
    *p = '';

    do {
    *--p = "0123456789abcdef"[num % base];
    num /= base;
    } while(num != 0);

    return p;
    }

    (Note that this function, as written, returns a pointer to static data, such that only one of its return values can be used at a time; . A better size for the retbuf array would be sizeof(int)*CHAR_BIT+1.)



    If you have the better answer, then send it to us. We will display your answer after the approval.
    Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?
    View Answer
    What is the most efficient way to count the number of bits which are set in an integer?
    View Answer
    What is the best way of making my program efficient?
    View Answer
    Are pointers really faster than arrays?
    View Answer
    I have been replacing multiplications and divisions with shift operators, because shifting is more efficient.
    View Answer
    People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed
    View Answer
    How can I swap two values without using a temporary?
    View Answer
    Which is more efficient, a switch statement or an if else chain?
    View Answer
    Is there a way to switch on strings?
    View Answer
    Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
    View Answer
    Are the outer parentheses in return statements really optional?
    View Answer
    Why dont C comments nest?
    View Answer
    Why isnt there a numbered, multi-level break statement to break out
    View Answer
    There seem to be a few missing operators ...
    View Answer
    Does C have circular shift operators?
    View Answer
    Is C a great language, or what?
    View Answer
    If the assignment operator were ...
    View Answer
    Does C have an equivalent to Pascals with statement?
    View Answer
    Why doesnt C have nested functions?
    View Answer
    What is assert and when would I use it?
    View Answer


    Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

    View ALL C Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2014 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0.015625 Seconds