How to Use bc as a Hex Calculator

Various Unix-like operating system implementations like Linux and pretty much anything that has the GNU suite includes the bc basic calculator language. Its syntax is very similar to the C programming language. You can use it as a hex calculator right from the command line, so you won’t ever need to download some hexadecimal or octal app for your phone again.

This is great news for developers and anyone working on some homework. You’ll need to open a terminal to do this though. Hold down Ctrl, Alt and T or search for the word terminal on the Ubuntu Unity Dash. You might want to start it from the Whisker or Applications menu under System Tools. Users of macOS can start a terminal from the Utilities folder or by clicking on the dock if it’s pinned. You can find bc on most Unix-like operating systems these days and not just in Linux.

Method 1: Using bc to Convert Decimal and Hexadecimal Numbers

Type echo ‘obase=16;127’ | bc to convert the number 127 to hexadecimal from regular base-10 numbers. Naturally, you can replace that with any whole number. The result you’ll get is 7F, and if you wanted to convert back to regular base-10 you could type echo ‘ibase=16;obase=A;7F’ | bc and push enter. It’s that simple and can be used anytime with any valid hex number as well. As it’s a valid Unix-style command, you could also incorporate this into any type of shell script. It’s easy to remember that you can use this from the command line at any moment you need to convert a number back and forth quickly.

Method 2: Converting Binary and Hexadecimal Numbers with the bc Hex Calculator

At the command prompt type echo ‘obase=16;ibase=2;111010001’ | bc to convert a binary number into a hex one. Push enter and you should get 1D1 as a reply. Naturally, you can fill in that binary line with any binary number that you needed converted at the time. Once again, too, the reverse is true and it’s possible to use the included hex calculator to convert a hex number into a binary one. Typing echo ‘obase=2;ibase=16;1D1’ | bc and pushing enter will you get your original number back, but you can replace 1D1 with other valid hex values.

If you use this often, then you’ll want to keep two points in mind. One is that letters inherent in hexadecimal numbers must always be upper case, because the bc software treats lower case letters as algebraic variables. The second is that all of these commands can be run by a regular user and you don’t need root access to do them on any modern version of Linux or Unix.

Method 3: Using bc as a Full Hex Calculator

You can also use the bc command as a hex calculator to perform arithmetic if you needed to. Most people won’t have this need, but those who are programmers might need to figure out the differences between two different locations in memory. This is also a really common question asked by those who are doing computer science homework for high school and college!

Let’s say you had one hex number that’s EE65522D and you wanted to subtract C3EFAF86 from it. You could type echo ‘ibase=16;EE65522D-C3EFAF86’ | bc and push enter to find the answer. If you’d prefer the answer be in hexadecimal, then you could try echo ‘obase=16;ibase=16;EE65522D-C3EFAF86’ | bc to solve it that way. Other arithmetic operations are allowed as well. For instance, echo ‘obase=16;ibase=16;EE65522D*C3EFAF86’ | bc would multiple the two values. You could of course use + as an operand for addition if you’d like. Using / for division doesn’t return a remainder; you’d need to use % for modulus in order to do that.

For instance, running  echo ‘obase=16;ibase=16;EE65522D%C3EFAF86’ | bc returns a value of 2A75A2A7, because the quotient of that equation is 1 remainder 2A75A2A7 when run through a hex calculator. Run the / operand first and then the % operand to find both values.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.