?
Solved

the format specifier for sprintf

Posted on 2001-08-27
24
Medium Priority
?
1,497 Views
Last Modified: 2013-11-20
I'm using sprintf(string,"%x",number) on a string to be displayed by a CStatic control.

where number is an int

can i display it in binary format? I don't find any binary specifier in the help ( i remember in Turbo C i have a %b specifier---something like that)

i mean i want to display a 2 as 10 in binary format, but not hexadecimal, octal or anythig else
0
Comment
Question by:orange_juice
[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
  • 11
  • 6
  • 3
  • +3
24 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 200 total points
ID: 6428699
Hi orange_juice,

for this you can use _itop function with argument 'radix' = 2,

hope that helps,

ZOPPO
0
 

Expert Comment

by:Kevin_Elrod
ID: 6429039
Convert the number to binary before you put it in the string.  That should be easier!
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6429179
> for this you can use _itop function with argument 'radix' = 2,

I think that was supposed to be "_itoa". :)

> Convert the number to binary before you put it in the string.

Gonna love to hear the details on that one! :)

-=- James.
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 6

Expert Comment

by:Triskelion
ID: 6430480
Here is a sample copied directly from the MS 16-bit compuler help file.  As 'Zoppo' meant and as 'jtwine' corrected, use _itoa():

/* ITOA.C: This program converts integers of various
 * sizes to strings in various radixes.
 */
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
   char buffer[20];
   int  i = 3445;
   long l = -344115L;
   unsigned long ul = 1234567890UL;
   _itoa( i, buffer, 10 );
   printf( "String of integer %d (radix 10): %s\n", i, buffer );
   _itoa( i, buffer, 16 );
   printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
   _itoa( i, buffer, 2  );
   printf( "String of integer %d (radix 2): %s\n", i, buffer );
   _ltoa( l, buffer, 16 );
   printf( "String of long int %ld (radix 16): 0x%s\n", l, buffer );
   _ultoa( ul, buffer, 16 );
   printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul, buffer );
}
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6431384
yes, sorry, a typo...

BTW, Kevin_Elrod, what exactly do you mean with 'Convert the number to binary before you put it in the string.'?
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432389
I must be way off, given the comments I've generated, but
what about this

str mybinary("01000010");


0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6432420
Kevin_Elrod, what should 'str' be? And what should it help to hardcode a string
which contains a binary number?
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432504
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432505
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432507
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432508
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432510
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432511
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432512
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432514
Okay, I spoke too soon.
0
 

Expert Comment

by:Kevin_Elrod
ID: 6432515
Okay, I spoke too soon.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6432531
And, maybe, too often          ;-)
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 6432572
Bad Refresh, Bad!  Now go sit in the corner, you bad Refresh, you! :P

---

> And what should it help to hardcode a string which contains a binary number?

Many people do things that seem weird.  Maybe the OP has his/her reasons...

> str mybinary("01000010");

If "str" is some kind of string object, then you have a string that contains "01000010", but you would still need to get the value of 66 converted to "01000010".

Besides, OJ's question has already been answered: _itoa with a Radix of 2 will convert a integer value to a binary string suitable for display.

-=- James.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6438697
A minor embellishement to the already complete answers...

It is common to need to display binary numbers in increments of eight digits.  If that is your need, try:

char buf[33]; // long enough for 32-bit number!
itoa( n, buf, 2 );

sprintf( str,"%08.8s", buf ) ; // eight bits
sprintf( str,"%016.16s", buf ) ; // sixteen bits
-=-=-=-=-=
or use this seldom-used idiom to set the width programmatically:

int nwidth= 8;
sprintf( str,"%0*.*s",nwidth,nwidth, buf ) ;

-- Dan
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6449815
wow, there has been lots of answer
i have been outstation, so today only i read all
this
i appreaciate everybody's help
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6450461
that does the job
  thank you to everybody too


how to retain those leading zeros when i use itoa
suppose i want a 000010 gets displayed instead of 10?

i think i should post this again

:)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6450469
well, as DanRollins mentioned above you can use the string you get from _itoa to create
a result as you want with i.e. sprintf( str,"%08.8s", buf )...
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6450471
... for 8 digits ... for the 000010 which is only 6 digits you can then use sprintf( str,"%06.6s", buf )
0
 
LVL 1

Author Comment

by:orange_juice
ID: 6455286
thank you again

:)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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