GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 5 of 258    Print  
How can I convert integers to binary or hexadecimal?

  
Total Answers and Comments: 4 Last Update: October 01, 2005   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: hari_it_205
 
we can convert the gn integer no into binary and hex no by using %x for binary and %e for hexadecimal in the printf statement instead of %d

Above answer was rated as good by the following members:
v.rajareddy
July 19, 2005 07:59:07   #1  
Tanushri        

RE: How can I convert integers to binary or hexadecimal?
we can convert integer to binary by deviding
integer no. by 2 & storing the no. in a variable

 
Is this answer useful? Yes | No
July 21, 2005 15:44:55   #2  
kartik        

RE: How can I convert integers to binary or hexadecimal?
u need to specify what base ur integer is in .... an integer could be a binary no or decimal ....
i htink its just a googly

 
Is this answer useful? Yes | No
August 13, 2005 12:07:26   #3  
hari_it_205        

RE: How can I convert integers to binary or hexadecimal?
we can convert the gn integer no into binary and hex no by using x for binary and e for hexadecimal in the printf statement instead of d
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 01, 2005 13:44:50   #4  
RichNistuk Member Since: October 2005   Contribution: 1    

RE: How can I convert integers to binary or hexadecim...

Here is my solution:

#include <iostream>

#include <iomanip>

#include <sstream>

using namespace std;

string toBinary(unsigned int num)

{

stringstream s;

for(int i 0; i<sizeof(int)*8; ++i){

s << (num&0x0001) ;

num >> 1;

}

return s.str();

}

void outLine(unsigned int num)

{

cout << setw(10)<< dec << num;

cout << bin: << toBinary(num);

cout << hex: << hex << setw(10) << num << endl;

}

int main()

{

unsigned int num 0;

outLine(num);

num 1;

while(num>0) {

outLine(num);

num * 2;

}

outLine(UINT_MAX);

cout << endl;

return 0;

}

Note the while loop makes use of the fact that if you start at 1 and double the number each loop then eventually the value will return to 0. Don't do this in real code!


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape