Thread: string to hex

  1. #1
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76

    string to hex

    I'm doing a game in SDL and the FillRect function takes a hex value (color) as a parameter.. I have scripts stored in external files with functions such as ClearScreen(#FFFFFF); .. I need to convert the "#FFFFFF" to real hex #FFFFFF .. thanks for your help

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You mean like 0xFFFFFF?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You can use sprintf() to do it, or you can use a stringstream.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    I don't think you really want to convert TO hex... You want to convert FROM hex.

    This can get confusing...

    Firstly, you don't want to change it to hex. What you really want to do is change the string-in-hex-format (#FFFFFF) to an integer (which has a value equal to FFFFFF hex).

    You can't really change a number to hex. Once a number gets into the computers memory its just a number, and all numbers are actually stored in binary. You can save it as a string that looks like a hex number, but it will be saved in binary numbers that represent the ASCII characters.

    This means that conversion only happens during input/output or when converting a string representation of the number (in some base) to a number, and vise-versa.

    Think about this:
    You can input an octal number, a decimal number, and a hex number. Later you can add the three numbers together without worring about the fact that they were input in different bases! You can display the result in any base you wish!




    Don't forget to strip-out the leading "#' character before converting.

    I don't have a stringstream example for you, but I think it's similar to this code fragment which treats an entered number as hex:
    Code:
    cout << "Enter a Hex number: ";
    cin >> hex >> x;
    Here's an example using the C function strtoul() (I think it's from <cstdlib.h> )
    Code:
    int x ;
    int Base = 16 ;
    char InputString[40]; 
    char *pEnd = NULL;          // Required for Strtoul()
    
    cout << "Number? ";
    cin >> InputString;
    x = (int)strtol(InputString, &pEnd, Base);     // String to long
    I might be missing something, but I don't think sprintf() will help... It can convert a number to a string... We're trying to go the other way 'round.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM