 # why use hexadecimal insted of binary?

I was wondering when and why we would use hex instead of binary

if computers only understand binary.. why would I choose to send a hex number down an SPI communication if the data sheet to a chip said to send 0's and 1's to pick if you are reading or writing what channel and power etc.

I ask this question as I keep seeing it done

Since everything ultimately resolves to binary, I think the answer is: Use what makes sense to you. Personally, if I'm doing bit fiddling and masking, I tend to use binary as it's easier for me to visualize what's going on. For reasons that I'm not sure of, I tend to use hex in very limited circumstances (e.g., using non-printing characters). Perhaps some of the assembly language gurus can shed some light.

Hex numbers are only a different representation and better to read than binary, but you still can see single bits in the pattern when you are used to it.

0x1458C vs 0b10100010110001100

Hexadecimal representation is more compact than binary and in many cases much clearer than decimal representation, especially when setting register contents.

I find it very convenient that each hex "digit" represents only four bits in one byte. That is not true of the digits in a decimal number (consider the difference between decimal 128 and 127, which have no "1" bits in common).

The truth is that computer programmers are a lazy bunch. Typing out the number in binary takes 8 keystrokes where typing it out in hex takes only two. The same exact thing goes out over the wire either way. People who do this work a lot will tend to learn the 16 bit patterns that correspond to 0-F and it becomes natural to write numbers that you think of in binary out in hex.

lifelight: I was wondering when and why we would use hex instead of binary

if computers only understand binary.. why would I choose to send a hex number down an SPI communication if the data sheet to a chip said to send 0's and 1's to pick if you are reading or writing what channel and power etc.

I ask this question as I keep seeing it done

You are not choosing to send HEX or BINARY, you are always sending BINARY.

But, humans read binary very slowly. We read decimal much more quickly.

So, if I ask you, what number do you want to send to the other side, you can either answer "01000001", or "65". Because I am human, I will understand "65" much more quickly. But, in both cases, you will be sending binary "01000001".

But, converting quickly between decimal and binary is not so easy. That's where HEXADECIMAL comes in. Any group of 4 binary digits has a range from 0000 to 1111. If you count the possibilities, that's 2 to the power of 4, which is 16. So, we give each of those possibilities a number. The first 10 are our normal 0 - 9, then we use A, B, C, D, E, F for the other 6.

Data is stored in computers in multiples of 8. That means, each group of 8 bits can easily be represented as 2 HEX digits.

So, in binary we go from "00000000" to "11111111". Giving every 4 bytes a hex number, in HEX we go from "00" to "FF". Every binary number quickly converts to hexadecimal because you just group 4 bits together and write the symbol. In decimal, those same numbers go from 0 - 255. Not so easy. Or for 16 bits, we go from "0000" to "FFFF" in hexadecimal, or 0 - 65535 in decimal. Hexadecimal is much quicker and more compact when we are talking about binary.

I wonder if say you were to have one byte of data to send over SPI to a dac before setting next two bytes for the power

how would it look if you wanted to choose

00 for write (0x0)

010 for data register (0x2)

001 for channel B (0x1)

would you send it as

Serial.write((0x0)+(0x2)+(0x1));//?

Serial.write((0x0),(0x2),(0x1));//?

Serial.write((int write)+(int data_register)+(int channel_B));//?

No it would have to be three separate calls to write.

``````Serial.write(0x00);
Serial.write(0x02);
Serial.write(0x01);
``````

When you use the + operator it adds them together. SO for instance your first example there would be equivalent to writing:

``````Serial.write(0x03);
``````

so writing them on separate lines like that would it shift the bits over to the left into the correct position of the byte if sending most significant bit first

00010001

"So, if I ask you, what number do you want to send to the other side, you can either answer "01000001", or "65". Because I am human, I will understand "65" much more quickly. But, in both cases, you will be sending binary "01000001"."

As an engineer, I would see that as 0x41, much easier to see what it represents vs 65. arduinodlb explained it very well.

I want to change channels etc so I can give each channel an address in hex or binary but if I used hex number how would I get it to sit in the correct place? do I still need to bit shift with hex to do this? just like I would with binary

is there an easy way to place a number where it should sit be it in binary or hexadecimal
so I can just say the numbers mapped to these come out in this order(write , data, channel, power)

so I can call what ever number is assigned to write/read
then call the data mode I want to use
pick my channel
and set the power to what ever is needed

16 is the number bigger than 10 that is an integral of 2. Saves keystrokes, easier to remember.

jremington: (consider the difference between decimal 128 and 127, which have no "1" bits in common).

Just to be pedantic... .which bits are common between 127 and 128 ?

None

Use a calculator to do the conversion if you can't imagine 127 and 128 as binary words.