Thread: Help with Hexa conversion

  1. #1
    schism
    Guest

    Help with Hexa conversion

    I've been trying to write a program that will accept two numbers from two different number systems (ie: Decimal, Hexa, Octa, or Binary)... Everything works except for the hexadecimal input... I know there's a way to set the output for an answer in hex, but is there any way to input a hexadecimal number from the keyboard, and convert it to decimal to add two numbers of a different base together?

    Example: 10(base 10) + 4e2f(base 16) = ??

    I really need to know this, anything would help...


    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sure, it's called a user defined function. In broad strokes accept input as a string. convert the values represented by each placeholder into the base system you wish to use. then convert the answer to whatever other base system you wish to use.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    1
    Ummm... Okay, maybe I should also add that I'm only as far as typedef in my Cpp class... We've done functions, but I don't know what a user-defined one is...
    Pretty much covered everything up to and including arrays... That's about it.

    Schism

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

    strtoul()

    We use strtoul()

    I don't know if it's ANSI, but it is included with the Microsoft Compiler. (And I forgot to look-up which library it's in.)

    here's a code fragment from an existing C program:

    Code:
    unsigned RtnValue, testdata, testdata2, datamask;
    char command[32], *ptr;
    
    gets(command);              // get command from kybd
    if(!strcmpi(command, "exit"))
            return 1;
    
    if(!strlen(command))
            datamask = 0xFFFF;
    else
            datamask = (int)strtoul(command, &ptr, 16);   //**** strtoul() ****
    
    printf("Data mask = %04X\n", datamask);
    [edit]
    strtoul() is ANSI and it's in stdlib.h ...of all places!
    [/edit/
    Last edited by DougDbug; 12-14-2002 at 12:19 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I have this problem of occassionally putting my foot in my mouth when I don't look things up I am not intimately familiar with before I post on the topic, but here goes anyway.

    int input = ABCE;

    where ABCE is a hexaddeicmal number, whatever it is, is likely to crash.

    preceding the value with Ox, or whatever, may save you, but I haven't verified that.

    using X in the format string of printf() doesn't really change the base of the output, it changes the presentation of the value.

    Not every base is covered by printf() format string options or by stream modifiers/manipulators. Not that you use bases other than binary, octal, decimal, or hex very often, but nevertheless, if you want to be able to convert from any given base to any other, then the available stuff don't work all the time and you have to write it yourself.

    functions you write yourself are called user defined functions.

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    schism from redmoon?

    is this schism from redmoon?
    nextus, the samurai warrior

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
       int num;
    
       cout << "Enter number in hex:";
       cin >> hex >> num;
       cout << "num:" << num << endl;
       cout << "num:" << hex << num << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of char for hexa!?!?
    By darkducke in forum C Programming
    Replies: 2
    Last Post: 05-29-2008, 09:40 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Char Hexa Conversion for socket raw!HELP ME PLEASE!
    By gls2ro in forum C Programming
    Replies: 1
    Last Post: 05-04-2004, 06:41 AM