Extracting hex values from character arrays

This is a discussion on Extracting hex values from character arrays within the Windows Programming forums, part of the Platform Specific Boards category; Hi using a Dialog based application under MFC I am trying to parse a character string array and total the ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Extracting hex values from character arrays

    Hi using a Dialog based application under MFC I am trying to parse a character string array and total the hex value of each character into a final total.

    I have checked the 18 pages of char to hex previous questions but can't seem to get the answer I want and most relate to C++ as well.

    Every time I get the character I seem to get the decimal value not the Hex value?

    I know the answer is trivial but well I am just going around in circles here trying whatever and hoping for no error on compiling but even then I can't seem to get the Hex value of the character.

    Any assistance most appreciated and thanks for your time.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    You're not going to get the answer you want because the conceptual basis of your question is fundamentally flawed.

    There is no difference between a hex value and a decimal value. They are the same value.

    Most of us have a total of twenty fingers, thumbs, and toes. The number of fingers, thumbs, and toes doesn't change whether we report that number as decimal (20), as hexadecimal (14), as octal (24), or as binary (10100). The value is the same regardless of the format in which it is reported.

    What is changing is the way that number is being printed out. In C++
    Code:
    #include <iostream>
    
    int main()
    {
        char i = 20;
        std::cout << std::dec << (int)i << " " << std::hex << (int)i << '\n';
    }
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    You use a combo of MFC and C IIRC.

    Have you tried....

    Code:
    _snprintf(output, len-1,"DEC:%d, HEX:%X",charTotal,charTotal);

    printf Type Field Characters (CRT)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    [QUOTE=novacain;1155877]You use a combo of MFC and C IIRC.

    Have you tried....

    Code:
    _snprintf(output, len-1,"DEC:%d, HEX:%X",charTotal,charTotal);
    IIRC sorry I don't understand what that means?

    Well I do this for enjoyment not employment and everything is self taught out of books etc. so please excuse the roughness.

    Anyway many thanks you put me onto the path I actually got it working for what I was trying to do please see below code and well it works any suggestions on improving it are most welcome.



    Code:
    GetDlgItemText(IDC_EDIT1, name, 80);
    
    len = strlen(name);
    
    for(i=0; i<len;i++)
    {
        _snprintf_s(char_entered, 2, "%X", name[i]);
     
        charin = atoi (char_entered);
    
        Running_Total += charin;
    }
    Thanks again for your time.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Scratch that it doesn't work sorry.

    The Running_Total += charin; line must be causing the problem.

    On entry to the loop for the name "Ice" first rotation puts 49 Capital "I" into Running_Total excellent.
    Second rotation charin contain "63" lower case "c" excellent.

    Added together should equal "AC" but is in fact 112.

    Gotta be something to do with signed or unsigned integers I'm thinking back to the drawing board.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    How about...

    Code:
    GetDlgItemText(IDC_EDIT1, name, 80);
    
    len = strlen(name);
    
    for(i=0; i<len;i++)
    {
        _snprintf_s(char_entered, 2, "%X", name[i]);
        //convert HEX to DECIMAL
        charin = strtol(char_entered,NULL,16);
        //add as DECIMAL
        Running_Total += charin;
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Quote Originally Posted by novacain View Post
    How about...
    Works perfectly thank you so very much

    Now another small problem I have come across I wish to add FF hex to a value so for example adding to 189 I can use "value -= 1" which works perfectly and will negate the value by one.

    If I wish to add FF to a much larger value eg. 8117D in assembler the result would be 8135 however I am unable to replicate this in C ?

    Again I am thinking the problem has to do with signed and unsigned numbers but hmm anyone have an idea on how to do this ?

    Thanks in advance for your time.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    Quote Originally Posted by ice_cracked View Post
    Again I am thinking the problem has to do with signed and unsigned numbers but hmm anyone have an idea on how to do this ?
    You need to specify what the assembler is doing. Of the operands FF, 8177D, and 8135, which ones are signed and which are unsigned? And what are the sizes?

    Adding FF being equivalent to subtracting 1 means you are (or were) using 16-bit unsigned operands (since operations are modulo 0x100 (= FF+1)). What happens with signed types depends on their representation (ones-complement, twos-complement, etc).

    If you wrote the assembler, that is information readily available to you. By not providing it, you are simply forcing anyone else to guess.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting series of hexadecimal values from buffer
    By Wole Fapohunda in forum C Programming
    Replies: 2
    Last Post: 09-06-2012, 12:11 AM
  2. extracting columns from arrays
    By kamasin in forum C Programming
    Replies: 14
    Last Post: 05-17-2011, 02:17 AM
  3. Extracting values...
    By AaA in forum C Programming
    Replies: 4
    Last Post: 07-26-2005, 08:43 AM
  4. Character Arrays
    By Smiley27 in forum C++ Programming
    Replies: 15
    Last Post: 04-13-2004, 04:19 PM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21