0

I'm working on an operating system related project now and there is one step which requires to check whether a hexadecimal number starts with 3 or not, using C.

Currently my idea is to convert that hexadecimal number into a string and check the initial character but just cannot find out any documentation for doing that. Anybody has any idea? Maybe just a hint.

user2736738
  • 30,591
  • 5
  • 42
  • 56
Boooooo
  • 157
  • 3
  • 12
  • What do you mean by a hexadecimal number? Are you just wanting to know if the most significant byte of an integer is 0x03? – William Pursell Mar 03 '18 at 00:06
  • Generally (to me anyway) if you have a "hexadecimal number" then you already have a string. Otherwise, you just call it an int. Or perhaps more specifically an unsigned int, or a uint32_t. – William Pursell Mar 03 '18 at 00:10
  • Converting to string is a terrible idea. Hex digits can be checked easily by bit masking. If your number is 8 bits, for example (2 hex digits), then checking if the first on is 3 is just `if (0x30 == (x & 0xF0))`. – Lee Daniel Crocker Mar 03 '18 at 00:34

2 Answers2

7

There are lots of methods to convert a number to a hex string (sprintf comes to mind; How can I convert an integer to a hexadecimal string in C? list a few more) – but, why should you?

A full hex number is formed by converting each nibble (4 bits) to a hexadecimal 'digit'. To get just the first, you can divide your value by 16 until you have reached the final (= 'first', in the left-to-right notation common for both decimal and hexadecimal values) digit. If that's a 3 you are done.

Assuming value is an unsigned number:

while (value > 15)
     value >>= 4;

and then check if value == 3.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • That's an elegant solution, now I'm kicking myself "why didn't I came up with this?" :) – Pablo Mar 03 '18 at 00:09
  • Exactly that one looks more "computer science", honestly I'm thinking why I did not come up with it hahaa – Boooooo Mar 03 '18 at 00:12
3

Your idea is not bad, you can use sprintf, it functions like printf/fprintf but instead of printing on screen (or to be more precise: writing into a FILE* buffer), it stores the contents in a char buffer.

char value[16]; // more than enough space for 4 byte values

int reg = 0x3eef;

sprintf(value, "%x", reg);

if(value[0] == '3')
    printf("The hexadecimal number 0x%s starts with a 3.\n", value);
Pablo
  • 13,271
  • 4
  • 39
  • 59
  • New knowledge got! Thank you for solving my problem – Boooooo Mar 03 '18 at 00:02
  • After converting your number to a string (why? that's not needed) why don't use it directly to save a little cpu with `printf("The.... 0x%s ...\n2, value);` so you don't convert the number again to a string. – Luis Colorado Mar 04 '18 at 19:41
  • @LuisColorado because my intention was to show how to use `sprintf` and I did not focus of minimizing cpu cycles. – Pablo Mar 04 '18 at 19:44
  • yes..... but you have done it once... why to do _the exact same conversion_ twice? – Luis Colorado Mar 06 '18 at 13:51
  • @LuisColorado like I said, my focus was on how to use `sprintf`, not on performance. I changed the `printf` to use `value` instead if `reg`, are you happy now? – Pablo Mar 06 '18 at 14:37