Bootstrap FreeKB - Linux Fundamentals - Convert a base 16 hexadecimal string to decimal in Linux
Linux Fundamentals - Convert a base 16 hexadecimal string to decimal in Linux

Updated:   |  Linux Fundamentals articles

A decimal string is a string that contains only integers, like this.

0123456789

 

A hexadecimal string (also known as "base 16" or "hex") is a string that contains integers and/or characters a through f, like this.

6a99b48e208ee4f6

 

The echo command can be used to convert a base string into decimal, like this.

echo $((6a99b48e208ee4f6))

 


Convert decimal to decimal

If the string contains only integers, and does not begin with a 0, the echo command will not change the string, as it is already a decimal string.

~]# echo $((123456789))
123456789

 

If the string contains only integers, and begins with a 0, the echo command will change the string.

~]# echo $((01234567))
342391

 

If the string contains only integers, and begins with a 0, once the string reaches a certain length (usually 8 or 9 characters), the 0x prefix will need to be appended to the beginning of the string.

~]# echo $((0x0123456789))
4886718345

 


Convert hexadecimal to decimal

The 0x prefix is always needed when converting a hexadecimal string to decimal. 

~]# echo $((0x6a99b48e208ee4f6))
7320993230861821174

 


Convert decimal to hexadecimal

~]# echo "obase=16; 7320993230861821174" | bc
6599648e208ee4f6

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c2420f in the box below so that we can be sure you are a human.