0

I saw may programmers using HEXADECIMAL number to describe value and I was wonder what is the BENEFIT of using HEXADECIMAL instead of regular decimal number.

For example :

<?php

fread( $myFile, 0x594 );
//vs
fread( $myFile, "1428" );

?>

And more importantly,

How do I get to that hexadecimal value ?

I was trying echo hexdec (1428); and I'm receiving 5160 and not 0x594.

What am I doing wrong here ?

Steven
  • 249
  • 5
  • 14
  • Hexadecimal (base 16) is useful in many ways, one of which is that it acts as a human-readable version of binary. – jub0bs Dec 07 '14 at 13:52
  • 2
    Hex maps to binary exactly. One hex digit is four bits, or half a byte (otherwise known as a nybble - they were hungry when they named them). Since computers tend to use binary-friendly sizes, hex is handy. – halfer Dec 07 '14 at 13:52
  • 1
    `dechex(1428)` is `0x594`; `hexdec(1428)` is `5160` – Mark Baker Dec 07 '14 at 13:52
  • OK why use Hexadecimal over regular decimal ? I assume that the decimal is more readable in most cases ? – Steven Dec 07 '14 at 13:53
  • 2
    The benefit of hex depends on the data that you're working with; RGB triplets (for example) `FF00FF` is more meaningful (Red FF, Green 00, Blue FF) to most people than 16711935 – Mark Baker Dec 07 '14 at 13:54
  • It's the mapping of one to the other that makes it helpful. As @Mark shows, specifying three bytes in hex is easy, since you know that it's three pairs of characters. It's not that easy in decimal. – halfer Dec 07 '14 at 13:55
  • Oh I thought that dechex will give me 0x results that why I was confusing... – Steven Dec 07 '14 at 13:56
  • For color it's obvious but for FILE SIZE ? – Steven Dec 07 '14 at 13:56
  • It all depends on the original data and what has more meaning to the developer.... perhaps the file has a documented specification that details everything in hex, including the length to read for a block header (for example).... look at the specs for many binary file formats (such as xls or ttf) and they typically detail the position in file and length of each field in terms of hex values – Mark Baker Dec 07 '14 at 14:00
  • [Why are flag enums usually defined with hexadecimal values](http://stackoverflow.com/q/13222671/995714), [Why use hex values instead of normal base 10 numbers?](http://stackoverflow.com/q/451886/995714), [Importance of Hexadecimal numbers in Computer Science](http://stackoverflow.com/q/16513806/995714), [Why do Computers use Hex Number System at assembly language?](http://stackoverflow.com/q/24198530/995714) – phuclv Mar 20 '16 at 15:07

1 Answers1

1

The hexadecimal number system became popular when microprocessors with 16 address bits arrived. A50F in hexadecimal is much easier to read an write than 1010010100001111 in binary. Binary is the only microprocessors understand. Also Hexadecimal takes up less memory space than decimal, very important in the early days of computing when memory was limited in size and very expensive.

castiron43
  • 11
  • 2