Hexadecimals and decimals, how to apply these properly?



  • Title really says it all.
    I have a number say 001520
    and I want to apply my checksum to it.

    where I split it into three blocks 00 15 20
    add the complimentary checksums, 00^FF, 15^FF, 20^FF -> 00FF 15EA 20DF

    The problem I am having is Im not too sure on how to apply this into Qt creator (Scientific calculator on windows gives correct answer).
    When I try to turn the decimal into hexadecimal it doesn't give correct answers.
    eg.

    @
    int hexval_1 = (15);
    QString hexval_1AsHex = QString("%1").arg(hexval_1, 0, 16);
    qDebug() << "Decimal = " << hexval_1 << " = " << hexval_1AsHex;
    @

    This spits out "f" when im looking for "EA"

    What im looking for is.. 15^FF in hexadecimal which will produce EA, Put when I apply the "^", I get FF was not declared.

    I googled this half the day but seem to be quite stumped?

    Anyhelp is appreciated!

    Regards

    Tazzi

    [EDIT: code formatting, please wrap in @-tags, Volker]



  • <code>int hexval_1 = 15</code> declares an integer variable containing decimal 15, which is hexadecimal F. If you want to declare a variable that contains hexdecimal 15, you will have to use <code>0x</code> notation.
    @
    quint8 hexval_1 = 15; // 00001111(2) 15(10) 0F(16)
    quint8 hexval_2 = 0x15; // 00010101(2) 21(10) 15(16)
    @
    For the one's complement C++ has its own operator, <code>~</code>.
    @
    quint8 hexval_1_complement = ~15; // 11110000(2) 240(10) F0(16)
    quint8 hexval_2_complement = ~0x15; // 11101010(2) 234(10) EA(16)
    @
    <code>^0xFF</code> is not the same as one's complement, as it does not respect the storage length.
    @
    int hexval_3 = 0x15; // 00000000000000000000000000010101(2) 21(10) 00000015(16)
    int hexval_3_complement = ~0x15; // 11111111111111111111111111101010(2) -22(10) FFFFFFEA(16)
    int hexval_3_xor = 0x15^0xFF; // 00000000000000000000000011101010(2) 234(10) 000000EA(16)
    @



  • [quote author="Lukas Geyer" date="1327480231"]
    For the one's complement C++ has its own operator

    quint8 hexval_1_complement = ~15; // 11110000(2) 240(10) F0(16)
    quint8 hexval_2_complement = ~0x15; // 11101010(2) 234(10) EA(16)
    [/quote]

    Thank you, I understand!
    Cheers for the examples as well, well appreciated.

    That "Pretty much" solved my problem! I get the right values and answers now.
    But I'v been trying to achieve this using variables.

    eg
    Your push a number in.. stick with 15..that is saved to a variable (An integer)

    Int Number= 15;
    quint8 hexval_2_complement = ~0x(Number);

    That is invalid and does not work (Being Int shouldnt affect?, but replacing with 15 will produce 234, thus can be converted to EA.

    I CAN do
    quint8 hexval_1_complement = ~Number; // 11110000(2) 240(10) F0(16)

    But as noted above it produces 240, which isn't exactly helpful.

    Sounds weird to obtain the int value then use that val as the hex val (as they dont equal each other).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.