Langsung ke konten utama

UNIX

Linux / UNIX: Convert Hexadecimal to Decimal Number

by on February 6, 2009 · 16 comments· last updated at November 23, 2010
How do I convert hex number to decimal number using a shell script under UNIX / Linux operating systems?

Hexadecimal (hex) is a numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen.

bc - An arbitrary precision calculator language

There is no need to write a shell script. You can simply use the following syntax at the shell prompt to convert hex to decimal number or vice versa.

bc: Hexadecimal or Binary Conversion

To convert to decimal, set ibase to 16, enter:
echo "ibase=16; hex-number"|bc
echo "ibase=16; FFF"|bc
Sample output:
4095
To convert to hexadecimal, set obase to 16, enter:
echo "obase=16; decimal-number"|bc
echo "obase=16; 10"|bc
Sample output:
A
ibase and obase define the conversion base for input and output numbers under bc. The default for both input and output is base 10. Add following function to your ~/.bashrc:
h2d(){
  echo "ibase=16; $@"|bc
}
d2h(){
  echo "obase=16; $@"|bc
}
The above two functions can be used from the command line as follows:
$ h2d 100
$ d2h AC

Base conversion using printf shell builtin

You can also use printf a shell builtin as well as /usr/bin/printf. To convert decimal to hex, you'd type:
printf "%x\n" 4095
Sample outputs:
fff
To convert hex to decimal, you'd type:
printf "%d\n" 0xfff
Sample outputs:
4095
You can save result to a shell variable and print it using printf or echo command:
output=$(printf "%d\n" 0xfff)
echo "${output}"
printf "%d\n" $output

Komentar

Postingan populer dari blog ini

Serba Serbi PPG SM3T 5 UNY

Pendidikan Profesi Guru. Zaman berganti, produk-produk manusia kian berkembang. Guru-pun tak luput dari jamahan perkembangan. Zaman dahulu, guru menjadi alternatif terakhir dari sebuah pekerjaan. Benar-benar sebuah pekerjaan panggilan hati. Mencerdaskan kehidupan bangsa berasaskan pancasila dan berpegang teguh pada nilai-nilai luhur menjadi prinsip utama dari pekerjaan ini. Lambat laun, pekerjaan guru semakin diperhitungkan. Kesadaran akan pentingnya sosok guru dalam kehidupan menjadi kian sentral. Insinyur, bupati, gubernur, teknokrat, dan presiden tak luput dari sentuhan guru yang mengilhami gerak dan tutur katanya. Pemerintah memahami hal ini, lambat laun "pekerjaan" menjadi guru dialihkan menjadi "profesi" dalam artian diharapkan menjadi guru professional, tangguh, dan siap di setiap situasi dan kondisi, mampu melayani peserta didik di setiap lini dan membantu untuk menggayuh cita-cita peserta didik. Guru perlu di"professional"kan kata Pemerinta...

EKONOMI SOURCES

Keseimbangan Pasar Proses Terbentuknya Harga Keseimbangan (Equillibrium Price) a. Pengertian Harga Keseimbangan             P ad a dasarn y a, prose s terbentukn y a harg a terjad i ketika tercapain y a tingka t keseimbanga n antar a permintaa n dan pena w aran.   Dapat   dikatakan   bah w a   harga   keseimbangan   atau harga   pasar   (equilibrium   price)   adalah   harga   y ang   terjadi   apabila jumlah   barang   y ang   diminta   sama   dengan   jumlah   barang   y ang dita w arkan.   Apab ila   ditunjukkan   dalam   bentuk   kur v a,harga keseimbanga n   merupaka n   perpotonga n   antar a   kur v a   permintaan   dan kur v a pena w aran. Pergeseran Grafik Keseimbangan Pasar Jika terdapat perubahan jumlah permintaan dan jumlah penawaran baik bertambah maupun berkurang...

Linux In Ipad

Siapa kira linux bisa berjalan di Hardware iPad....       Good news for fans of Tux the penguin : soon you’ll be able to run the Linux operating system on your iDevice. A team of jailbreak developers has managed to run the open source Linux OS on Apple’s A4-powered devices.       Who needs Android when you have Linux, right? BluerisEN tweeted the news with a picture of Linux running on an iPad. The iPhone 4, first gen iPad, and 4th generation iPod touch will all be able to run this specific build of Linux. The iPad 2 will not be compatible, due to the fact that it runs on the A5 processor. Hackers have been able to run doctored builds of Android on iDevices before, but the addition of Linux adds more possibilities to current Apple hardware that will become outdated in years to come. Installing Linux will require a jailbreak , however.      There hasn’t been an ETA set for the availabilit...