Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do they convert Decimal to Hexadecimal so fast (in mind)?

I've observed few reverse engineers, they convert decimal to hexadecimal so fast in mind. It's simply amazing. I never got chance to ask them. Personally, I really suck it this conversion and I always use a calculator for conversion.

I was wondering if there is some kind of short cut for this conversion?

I think especially for a reverse engineer & a low level (Assembly, Embedded) programmer. Its a BIG PLUS if he can count, add, subtract and think in terms of HEX instead of decimal. If you have any tips for this, kindly share.

like image 559
claws Avatar asked Feb 03 '23 00:02

claws


2 Answers

You need to know the basic conversions 0-16, 0x0-0xF and 0b0-0b1111 conversions by hart.

The rest you learn with repetition. The are some often repeated patterns to watch for.

Multiples:

  • 1024(1K) is 0x400
  • (1024*1024)1048567(1M) is 0x100000
  • just multiply with 4 to get the size of 4M as 0x400000.

Similar for bit positions you can learn the decimal values

  • The MSB of a 16 bit word is 0x8000 or 32768 or 32K
  • Thus next bit has a value is 0x4000 or 16384 or 16K

These patterns repeat everywhere and with time you will start to learn them.

If you have a binary representation it is easy to group the bits in groups of four and quickly convert to a binary representation.

The only realistic way to find the decimal value 0xA1B587DE is to use a calculator(or be unbelievably good at maths). But the nearest 1k boundary down from 0xA1B587DE is 0xA1B58400 which is easy if you know the patterns.

From your comments on opcode:
For RISC processors most instructions just the first few bits in an instruction word defines the family of instruction (mov, jump, and, or, ...) and the rest is just parameters. If you work with a processor enough you will start to learn them.

like image 131
Gerhard Avatar answered Feb 05 '23 16:02

Gerhard


It is quite simple once you understand the base of the numbering systems involved. Hexadecimal is base 16 - so, 1(dec) = 0x01; but 16(dec) = 0x10.

whenever you see a decimal number, say, 39:

divide it by 16 and just take the quotient - this is 2 (2*16 = 32) remainder is 7 ( 39 - 32 )

The HEX value of decimal 39, is therefore: 0x27.

Now, convert it back to decimal: 0x27 = 2*16 + 7 = 39 decimal :)

Hope you got the idea!!

like image 23
ZacharyLewis Avatar answered Feb 05 '23 17:02

ZacharyLewis