are you asking about %x
Main Topics
Browse All TopicsHow to printf integer, byte etc in their binary format?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
This is a function I wrote sometime back, can be used to print in any base(between 2 and 36).
for base >36, we need more symbols than just numbers and alphabets.
maxwidth is the size of the dest buffer.
char * toString(int num, char * dest, unsigned radix, unsigned maxwidth)
{
if(!dest)
return dest;
if (radix < 2 || radix > 36)
{
dest[0] = 0;
return dest;
}
int out = num;
unsigned len = 0, i =0;
int rem;
while(out&& len<maxwidth)
{
rem = out % radix;
out = out/radix;
if (rem > 9)
dest[len] = (char)(rem - 10) + 'a';
else
dest[len] = (char)rem + '0';
++len;
}
if(!len)
dest[len++]='0';//for number 0
dest[len] = 0;
// reverse digits
for (i = 0; i < (len >> 1); ++i)
{
char c = dest[i];
dest[i] = dest[len-1-i];
dest[len-1-i] = c;
}
}
>how should the flag be interpreted?
Something like convertToBinary(char omitZeroflag...
and check the omitZeroflag to decide whether we want to omit zero...
Since this is just a on and off switch..in Java we can use boolean, but I don't really confirm that what is the best way to implement in C(Can someone tell me what is the best way to do a on and off switch in C?). Anyway, I think I have got many good idea to get the binary here, thanks for the question:)
>>How are you deciding how many leading 0s are in the binary form?
You did not mention the number of zeros you want. (there is no straight answer to this, it is best not to display any leading zeros or always display fixed width numbers; say 32 or 64 digits)
BTW, the function I posted works for unsigned numbers, negative numbers will get printed in 2's compliment format.
There is a miss-by-one error in the while loop condition,
it should be
while(out && len<maxwidth-1)
>Can someone tell me what is the best way to do a on and off switch in C?
usually an int is used(0 = false, anything else = true)
if you include stdbool.h, then you can use the bool datatype in C.
however, for most systems bool is not guaranteed to be 1 byte (can be as large as 8 bytes on some old systems),
till sometime back, the only thing you could assume was that size of bool <= size of long.
Though on latest versions on gcc, it is 1 byte.
if memory is an issue, char should be used. if it is a big issue and a lot of bools are needed, use each bit of an int as a bitmap.
if there are only a few bool on modern systems with memory running into GBs, it will not even me measurable, so does not matter at all.
however, i think the idea of using int where bool could be because:
-IMHO int as greater readability than char when used as bool (char 0 is non printable, while int 0 is)
-int used to be the default(implicit) datatype for lot of cases in C(not sure about this though)
Business Accounts
Answer for Membership
by: ozoPosted on 2006-10-22 at 20:29:04ID: 17786309
what do you mean when you say "in their binary format"?