Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hexidecimal

Status
Not open for further replies.

Scooby316

IS-IT--Management
Jan 30, 2003
32
0
0
US
Can anyboy tell me what the hexidecimal values are for -17 and -13. Can't find after to help me convert to a minus.

Cheers
 
Search for "representation of negative numbers".
The easiest way to consider negative numbers is to imagine you have a variable that can contain only two decimal digits, 00 - 99.
If you add 99 to some value, say 3, you'll get 102. But there isn't room for the third digit, so actually you'll see 02 (and a carry, but we can forget about that).
But 02 is one less than 3! So we've subtracted one.
-1 is therefore representable by (maximum number + 1) - 1 (because we've just done the sum 3 + 99 = "2" and we want to do 3 + (-1) = 2).

Therefore the hexadecimal of -17 depends on what size variable you're working with. For a byte, the hexadecimal for -1 would be 0FEh; for a 32-bit thing it's 0FFFFFFFEh.
You can work out -17 and -23 in the same way.

Hope that makes sense.
 
So sorry, of course I meant the (byte) hexadecimal of -2 is 0FEh. 0FFh is -1. I should read before clicking "send"
 
Simple to figure out.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define hexconvert(int1,str) ((sprintf(str,"%x",int1)))








int main(int argc, char **argv) {
int p, v;
char numstr[10];

                if (argc == 2) {
                   p = atoi(argv[1]);
                   hexconvert(p,numstr);
                   printf("Hex = %s\n",numstr);
                }
return 0;
}
 
It seems the simplest answer was (for 32 bits ints):
Code:
#include <stdio.h>
int main()
{
  printf("Hex -17 == %X, -13 == %X\n",-17,-13);
  return 0;
}
...
Hex -17 == FFFFFFEF, -13 == FFFFFFF3
and so on...
Don't forget to add 0x prefix in sources...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top