Results 1 to 8 of 8

Thread: Hex function in C++ ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109

    Hex function in C++ ?

    Is there a function in C++ like VB's Hex function? ... I dont feel like writing my own

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    how about sprintf with "%X"

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    string Hex(unsigned long num) {
        ostringstream oss;
        oss << hex << num;
        return oss.str();
    }
    
    int main() {
        unsigned long number = 0xDEADBEEF;
        unsigned long another = 0x1COFFEE1;
    
        cout << "0x" << hex << number << endl;
        cout << "0x" << Hex(another) << endl;
    }
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    only problem is.... i want to put the hexed result into a char array.

    I need something like...

    Hex(int number, char* result);

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    Ah, just noticed the sprintf post..... I forgot all about sprintf

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You asked for C++, so that's what I gave you. Why a char array? What's wrong with a string?
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    I looked at your code to fast.... thought it did something i didnt want

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    parksie, you forgot
    0x1BADF00D
    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