$ let x=0x3f $ echo $x 63 $ let x=0xfffe $ echo $x 65534[robg adds: Not much call for hex converters in my line of work, but I thought some of you might find this useful...]
|
|
|
If you have a value in hex format and want to see its decimal value, you can easily convert it using the terminal. To convert 3f hexadecimal type (in a bash shell):
$ let x=0x3f $ echo $x 63 $ let x=0xfffe $ echo $x 65534[robg adds: Not much call for hex converters in my line of work, but I thought some of you might find this useful...]
•
[67,742 views]
Hint Options
Call bash non-interactively
If you're using tcsh, rather than spawning a bash subshell for this, you can invoke bash non-interactively with a "bash -c '<command>'". Thus:
Try bc(1) too
bc is also able to do radix conversions in just about any radix (expressible in numerals and uppercase letters). For example, for hex to decimal...
tcsh% bc ibase=16 3F 63 [ctrl-D] tcsh% Or, for a shell 1-liner... tcsh% echo "ibase=16; 3F" | bc 63 tcsh%Defining a csh alias or sh function named hex2dec is left as an exercise for the reader :^)! Note (1) you have to use uppercase, (2) once you specify a non-decimal input base (via ibase=...) ALL your input is in the new radix until you change it. bc is pretty powerful, capable of arbitrary precision math, function definition, complex C-like flow control expressions, etc. See the man page for details.
Don't forget obase
You can convert from, say, octal to hex by:
Note that obase is first since ibase changes your input base for all following commands, including the obase command if you reversed the order.
A simple bash shell hex converter
isntead of two commands, you can do it using arithmetic expression interpolation:
/usr/bin/printf
printf is a bit more flexible, you can convert from hex back into decimal,
A simple bash shell hex converter
Well, being from a linux environment, two possibly easier methods come to mind:
Perl version
Here's how you could do it as a Perl one-liner:
perl -e 'print 0x3f, "\n"'
or
perl -e 'printf("%d\n", 0x3f)'
backslashes (sigh)
The last "n" in both version is supposed to be "\\n" - i.e. backslash-n.
A simple bash shell hex converter
Two bash aliases that might help out...
A simple bash shell hex converter
now, when did bash start allowing command line args in aliases?
And in Python
Python is included with 10.2:
% python -c "print 0x3f"
A simple bash shell hex converter
Well, i just found a way that is simple enough to actually remember. Type in "bc" w/o the quotes, then return in the default shell(i dont know about ne others) then change the variables "ibase" and "obase" to suit ur input and output respectivly. I.e: |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.54 seconds |
|