Results 1 to 7 of 7

Thread: Converting Bases in c++

  1. #1
    ChimpFace9000
    Guest

    Post Converting Bases in c++

    To turn a number in ascii in any base in c i would use sprintf. Whats the c++ equivelent?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Is this the sort of thing you mean:
    Code:
    cout << hex << 100 << oct << 255 << endl;
    ?

    Or did you want something for any random base? Doesn't itoa have a radix argument?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    yeah it does

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

  4. #4
    ChimpFace9000
    Guest

    Post

    Thnks parksie but i need to do it into a buffer. Its for a windows app.

    Isnt itoa a c function?

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    works for me in windows programs...thats the only way I've been taught to output numbers in windows

  6. #6
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Originally posted by parksie
    Is this the sort of thing you mean:
    Code:
    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)

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    To get it into a buffer use stringstream (header <sstream>, for <string> objects) or strstream (header <strstream>, for char* C-style strings).

    Code:
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width