[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Base conversion within Base
From: |
Eduardo A . Bustamante López |
Subject: |
Re: Base conversion within Base |
Date: |
Wed, 17 Sep 2014 08:11:13 -0700 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
> That said, I have no objection to Chet adding such a feature to Bash,
> if he can think of a sane way to do it. If you have a patch that
> implements it, it's possible he'll be willing to review it.
I don't really see the point, that'd be just bloating the code base. That task
is easy to accomplish using the features that bash already offers:
address@hidden ~ % cat foo
convert() {
local base=$1 numerals=$2 number=$3
local result
while ((number >= base)); do
result=${numerals:number % base:1}$result
((number = number / base))
done
result=${numerals:number:1}$result
echo "$result"
}
convert 23 0123456789abcdefghijklm 1040
convert 16 0123456789abcdef 255
convert 6 012345 123
convert 2 .- 10
convert 2 01 5
convert 8 01234567 16
address@hidden ~ % bash foo
1m5
ff
323
-.-.
101
20
The point of features is to accomplish things that are not already
possible, or that are possible, but with an insane amount of work. An
8 line function does what you want, with the features already
available.