Convert hex to decimal and vice versa

Tip 194, on page 233, explained how the bc can be used to create a script that can do simple math at the command line. As mentioned, bc has many features. One of the many other things it can do is convert decimal to hex, and vice versa (if you're wondering what hex is, you might want to skip this particular tip). To convert from decimal to hex, start bc by typing bc at the command line and then type obase=16. Then type the number you want to convert.

To convert hex to decimal using bc, start bc and type ibase=16. Then type the hex number, ensuring that A, B, C, D, E or F are all typed in uppercase.

As with the earlier tip, a simple script can be created to carry out the conversion at the command-line. To create a script that converts decimal to hex, open Gedit and create a file called dec-to-hex. Then type the following:

# Take hex input and run it through bc for decimal output option= 'obase=16; ' convert=${option}[email protected] echo $convert|bc

Save the file and close Gedit. Then mark the script as executable and then copy it to the /usr/bin folder, so that it will be available for all users, as follows:

$ chmod +x dec-to-hex $ sudo mv dec-to-hex /usr/bin/

Following this, the script can be used from the command line, in a way similar to this:

To create a similar home-made command that converts hex to decimal, repeat the above steps but call the new script hex-to-dec and, in the third line of the script, type option='ibase=16;' instead. You will need to change the comment on the second line too, so it explains what the script does. Then follow the instructions to mark the script as executable and copy it to /usr/bin, substituting the different filename for dec-to-hex. The new command than then be used from the command-line in the same way (for example, hex-to-dec FFC21A).

Ubuntu's default Calculator application (Applications ^ Accessories) can also convert between hex and decimal. To do so, select Scientific on the view menu. Then, if you want to convert a decimal number to hex, type it and click the Hex radio button. To convert a number from hex to decimal, select the Hex radio button. Then type the hex number and click the Dec radio button.

Did you know that if you click and drag a picture on a website to your desktop, it will be automatically copied across? Unfortunately, this doesn't work if the picture is also a link—in that case, a new file will be created that, when double-clicked, opens the link in the web browser. This also works in reverse with a new Evolution email—drag the image onto a blank spot of the new email text area, and it will be automatically attached.

For more Firefox-related tips, see Tip 7, on page 66; Tip 55, on page 118; Tip 64, on page 124; Tip 69, on page 128; Tip 163, on page 202; Tip 204, on page 239; Tip 213; and Tip 285, on page 332.

Nearly all of us spot links whilst browsing that we want to send to friends. Usually we have to cut and paste the email address into a new email, which can take quite a few steps. An easier process is, within Firefox, to click File ^ Send Link. This will create a new email with the link in the body of the email, and a suitable subject line. All you have to do is fill-in the address.

If even that sounds like too much trouble, install the Email This! Firefox extension from https://addons.mozilla.org/en-US/firefox/addon/3102. This is better in some regards because it lets you also send text from the webpage. Once it's installed (and Firefox has restarted), highlight the rel evant text in the page and then right-click anywhere in it. Select Email This! and then select Mail-To This! (Windows/OS-X/Linux). This will create a new Evolution email with the link in the body of the mail, along with the highlighted text and the subject filled in automatically. If you don't want to send any text from the page, just right-click without first highlighting.

Was this article helpful?

0 0

Post a comment