Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Posted: 3/13/2006 8:16:45 PM EDT
My brain is fried so here's a stupid question. I'm simply trying to display a number in binary form using setbase. Here's all it is.

int val = 0;
cout << "Enter a number: ";
cin >> val;

cout << setbase(2) << val << setbase(10) << endl;

The first setbase command sets the base to 2 (presumably to display the number as binary) and the second setbase command converts the base back to 10, since its not needed beyond displaying the number.

All I get spit back out at me is the number I enter is regular integer form, not in binary.

What am I doing wrong?

Sorry, brain is toast and can't figure out wtf is going on.
Link Posted: 3/13/2006 8:18:43 PM EDT
[#1]

Link Posted: 3/13/2006 8:25:21 PM EDT
[#2]
From a quick search, I think that setbase() can only take values 8, 10, or 16.
Link Posted: 3/13/2006 8:27:33 PM EDT
[#3]

Quoted:
From a quick search, I think that setbase() can only take values 8, 10, or 16.



Now that could certainly be the problem.   I wish I had the help program properly installed on my C++ compiler.
Link Posted: 3/13/2006 8:28:05 PM EDT
[#4]
It's been awhile since I messed with C++ but I don't think setbase does binary.
Link Posted: 3/13/2006 8:36:48 PM EDT
[#5]
Hmm....my professor seemed to imply there was some lovely little function you ran an integer through and it spit out a binary number.....perhaps such a thing does not exist. I have been unable to get a clear answer of it off of google.......
Link Posted: 3/13/2006 8:39:43 PM EDT
[#6]
bah, nm........already mentioned, left the computer for too long
Link Posted: 3/13/2006 8:44:05 PM EDT
[#7]
There is no simple way to print out the binary form of an integer in C++. You need to write your own function like this one (found online) that takes in an integer and returns a string of the bits:



private string ToBinary(Int64 Decimal)

{

  // Declare a few variables we're going to need

  Int64 BinaryHolder;

  char[] BinaryArray;

  string BinaryResult = "";


  while (Decimal > 0)

  {

     BinaryHolder = Decimal % 2;

     BinaryResult += BinaryHolder;

     Decimal = Decimal / 2;

  }


  // The algoritm gives us the binary number in reverse order (mirrored)

  // We store it in an array so that we can reverse it back to normal

  BinaryArray = BinaryResult.ToCharArray();

  Array.Reverse(BinaryArray);

  BinaryResult = new string(BinaryArray);


  return BinaryResult;

}



This is starting to bring back bad memories of my computer science classes. Luckily, I will escape with a degree this May!
Link Posted: 3/13/2006 8:46:25 PM EDT
[#8]
itoa() ??
Link Posted: 3/13/2006 8:52:18 PM EDT
[#9]

Quoted:
There is no simple way to print out the binary form of an integer in C++. You need to write your own function like this one (found online) that takes in an integer and returns a string of the bits:



private string ToBinary(Int64 Decimal)

{

  // Declare a few variables we're going to need

  Int64 BinaryHolder;

  char[] BinaryArray;

  string BinaryResult = "";


  while (Decimal > 0)

  {

     BinaryHolder = Decimal % 2;

     BinaryResult += BinaryHolder;

     Decimal = Decimal / 2;

  }


  // The algoritm gives us the binary number in reverse order (mirrored)

  // We store it in an array so that we can reverse it back to normal

  BinaryArray = BinaryResult.ToCharArray();

  Array.Reverse(BinaryArray);

  BinaryResult = new string(BinaryArray);


  return BinaryResult;

}



This is starting to bring back bad memories of my computer science classes. Luckily, I will escape with a degree this May!




Think that's bad, I had to write a program that did it in assembly last sememster.
Link Posted: 3/13/2006 8:55:31 PM EDT
[#10]

Quoted:
itoa() ??



Damn, forgot all about that.
Link Posted: 3/13/2006 8:57:57 PM EDT
[#11]

Quoted:

Quoted:
There is no simple way to print out the binary form of an integer in C++. You need to write your own function like this one (found online) that takes in an integer and returns a string of the bits:



private string ToBinary(Int64 Decimal)

{

  // Declare a few variables we're going to need

  Int64 BinaryHolder;

  char[] BinaryArray;

  string BinaryResult = "";


  while (Decimal > 0)

  {

     BinaryHolder = Decimal % 2;

     BinaryResult += BinaryHolder;

     Decimal = Decimal / 2;

  }


  // The algoritm gives us the binary number in reverse order (mirrored)

  // We store it in an array so that we can reverse it back to normal

  BinaryArray = BinaryResult.ToCharArray();

  Array.Reverse(BinaryArray);

  BinaryResult = new string(BinaryArray);


  return BinaryResult;

}



This is starting to bring back bad memories of my computer science classes. Luckily, I will escape with a degree this May!




Think that's bad, I had to write a program that did it in assembly last sememster.



pfft, this semester I'm doing a group project involving writing a standalone operating system. Writing device drivers and messing with control registers .

Assembly I would think would be easy because you could just rotate through the carry and print a 1 if the carry bit is set 0 if not. Simple.
Link Posted: 3/13/2006 9:07:04 PM EDT
[#12]
Try this maybe?


int val = 0;
cout << "Enter a number: ";
cin >> val;

setbase(2)
cout << val << endl;
setbase(10)

Link Posted: 3/13/2006 9:09:41 PM EDT
[#13]

Quoted:
Assembly I would think would be easy because you could just rotate through the carry and print a 1 if the carry bit is set 0 if not. Simple.



Basically. I forgot though, it was hex to binary.


ORG $1000
HEX_IN  FDB $F23C
DEC_WTS FDB 10000,1000,100,10,1

ORG $1010
DEC_OUT FCC "00000"
*******************************

ORG $2000

LDY #DEC_OUT     ;point to the ascii array
LDX #DEC_WTS     ;point to the array
LDD HEX_IN       ;load the number being converted
START CPD #$0000        ;compare to zero
BEQ FIN          ;if = to zero, we're done
CLC              ;clear the c bit
INC 0,Y          ;increment the ascii array
SUBD 0,X         ;subtract the number being pointed to from the number being converted
BCS FIX          ;if there is a carry, we went to far so go fix it
BCC START        ;if there is no carry, do it again

FIN SWI              ;done

**********************************

FIX ADDD 0,X         ;add the last number subtracted back
DEC 0,Y          ;decrement the ascii array
INX              ;increment x to point to the next number to sutract
INX
INY              ;increment y to the next poition
BRA START        ;go subtract




and I guess it wasn't as bad as I remembered it.

ETA: we need CODE tags.
Link Posted: 3/13/2006 9:14:38 PM EDT
[#14]

Quoted:
Hmm....my professor seemed to imply there was some lovely little function you ran an integer through and it spit out a binary number......



Oh it exists...

String Integer.toBinaryString(int i);

Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top