Solved

printf int in binary format

Posted on 2006-10-22
15
22,949 Views
Last Modified: 2013-11-15
How to printf integer, byte etc in their binary format?
0
Comment
Question by:william007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17786309
what do you mean when you say "in their binary format"?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17786331
are you asking about %x
0
 
LVL 9

Author Comment

by:william007
ID: 17786338
Hi, let say int i=4;
%x will be printing as hex form 4
But what I want is the binary form which is the 0100.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 45

Accepted Solution

by:
sunnycoder earned 300 total points
ID: 17786360
0
 
LVL 84

Expert Comment

by:ozo
ID: 17786363
there is no standard printf format to do that.
you could write a function to do that.
How are you deciding how many leading 0s are in the binary form?
0
 
LVL 9

Author Comment

by:william007
ID: 17786491
Hi ozo,
For the leading 0s I will consider omitted it.
But if we must write a function, then I would suggest to make it as a flag in the argument.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17786542
how should the flag be interpreted?
0
 
LVL 2

Assisted Solution

by:avsrivastava
avsrivastava earned 200 total points
ID: 17786900
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;
        }
}
0
 
LVL 9

Author Comment

by:william007
ID: 17787439
>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:)

0
 
LVL 2

Assisted Solution

by:avsrivastava
avsrivastava earned 200 total points
ID: 17787475
>>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)

0
 
LVL 2

Expert Comment

by:avsrivastava
ID: 17787495
>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.
0
 
LVL 9

Author Comment

by:william007
ID: 17789040
>usually an int is used(0 = false, anything else = true)
Hi, I am curious why don't we use char...since int is 2 bytes(short integer, generally) but char is 1 byte, which can save more spaces?
0
 
LVL 2

Assisted Solution

by:avsrivastava
avsrivastava earned 200 total points
ID: 17789736
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)
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 300 total points
ID: 17789805
Typically chars indeed are used for bool. 1 (non zero or !0) being true and 0 being false. chars are internally stored as ints. To print 0 and 1, use %d flag instead of %c - this would print the ASCII value of the char.
0
 
LVL 9

Author Comment

by:william007
ID: 17794373
Thanks=)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question