How can I convert integers to binary or hexadecimal?
Submitted by: AdministratorMake 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:
Submitted by: Administrator
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:
Submitted by: Administrator
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.)
Submitted by: Administrator
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.)
Submitted by: Administrator
Read Online C Programming Job Interview Questions And Answers
Top C Programming Questions
☺ | Why does this code crash? |
☺ | What is wrong with this declaration? |
☺ | How can I read/write structures from/to data files? |
☺ | What does static variable mean in c? |
☺ | How can I increase the allowable number of simultaneously open files? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |