PDA

Click to See Complete Forum and Search --> : Converting Bases in c++


ChimpFace9000
Mar 8th, 2002, 10:47 PM
To turn a number in ascii in any base in c i would use sprintf. Whats the c++ equivelent?

parksie
Mar 9th, 2002, 05:04 AM
Is this the sort of thing you mean:cout << hex << 100 << oct << 255 << endl;?

Or did you want something for any random base? Doesn't itoa have a radix argument?

SteveCRM
Mar 9th, 2002, 09:25 AM
yeah it does

itoa(number, string it goes in, base you want);

ChimpFace9000
Mar 9th, 2002, 11:26 AM
Thnks parksie but i need to do it into a buffer. Its for a windows app.

Isnt itoa a c function?

SteveCRM
Mar 9th, 2002, 05:41 PM
works for me in windows programs...thats the only way I've been taught to output numbers in windows

sail3005
Mar 9th, 2002, 07:12 PM
Originally posted by parksie
Is this the sort of thing you mean:cout << hex << 100 << oct << 255 << endl;?

Or did you want something for any random base? Doesn't itoa have a radix argument?

what are the things for the different bases? i tried bin but it didn't work (for binary)

CornedBee
Mar 10th, 2002, 06:40 AM
To get it into a buffer use stringstream (header <sstream>, for <string> objects) or strstream (header <strstream>, for char* C-style strings).


stringstream ss;
ss << hex << 100 << oct << 255 << endl;


There seems to be no way to choose any base, setbase only accepts 8, 10 and 16 as arguments.