Nice script to convert numbers between bases (bin/dec/oct/hex)
I had to write this script for an assignment, and it took me hours of pulling out my hair to figure it out (I'm new to shell scripting), so I thought I'd post it. I know it's probably not the most efficient way to do it, since a CASE would probably be better, but it works for me. :rolleyes:
Basically it converts numbers of one base (I.E. hex numbers like 2F) to another base (I.E. binary or decimal or octal). Let me know if you like it, or if there's a way to improve it. Code:
#!/bin/bash |
This is the way I'd do it:
Code:
#!/bin/bash Code:
bconvert -o 3 108762345243 42352453 524352453 What could be enhanced? Well, the error handling could be better. Also you could add a -h|--help clause in the case statement. The help text could be designed as a function (you already have two examples in that code), a function that should be called either when you use -h or --help or when you invoke the program without arguments. Designing it as a function let's you call it from many places without having to duplicate the code. You could as well add -idec, -ihex, -ibin, -ioct, -ooct, -odec, -ohex, -obin parameters that would be shorthands for these common bases. This is a quick adaptation so it might have some flaws that I haven't yet considered. EDIT: fixed. |
This is good information, thanks a lot. I knew there had to be a way to do it with CASE statements, but I haven't covered them yet and I don't know how to use them. Thanks for the great example.
|
Sorry, but I'd do this in C not bash.
|
Another way
Code:
#!/usr/bin/perl 00000011 ./script B 00001111 H 0f floats to native ieee representation: ./script f 1.1 H cdcc8c3f |
All times are GMT -5. The time now is 04:00 PM. |