Advertisement

binary numbers

Started by March 29, 2002 01:38 PM
2 comments, last by nextgengamer 22 years, 7 months ago
Hi all i was wondering if there was an easy way to get some number to display as a binary number using MSVC++ with a win32 console. i really need something like this: int x = 12; cout << "Your number is : " << binary(x) << ''\n''; I dont think there is a way to typecast it though. Once again any help would be appreciated. Thanks NextGenGamer
** NOTE: this is a non ANSI C function; denoted by the leading "_" **.

here you go....


  int main(void){  char buf[34];  int  i = 12;  memset(buf, 0, 34); // clear array.  _itoa(i, buf, 2);   // radix of 2 for binary.  printf(buf);        // display number.  printf("\n");       // newline.  return (0);         // return 0.}  



for more information on "_itoa".

_itoa

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Advertisement
Ahw, I knew there had to be a simpler way. I wrote my own base 10 to binary converter when I was assigned to make a program that counts every numbers from 0-255 (in binary) that begins and/or ends with 11.
Oh well, I guess I learnt something doing so, even if it took a bit longer.
What do you think about this :


      #include <stdio.h>void print_binary(int);void main(void){	print_binary(59);	print_binary(-59);	print_binary(0);	print_binary(1);}void print_binary(int number){	unsigned int mask=0x80000000;	do	{		if(number & mask) printf("1");		else printf("0");	}	while(mask>>=1);	printf("\n");}    


Is it what you want?

I know that I don''t know nothing... Operation Ivy
I know that I don't know nothing... Operation Ivy

This topic is closed to new replies.

Advertisement