Displaying 3 results from an estimated 3 matches for "hex_to_dec".
2015 Aug 31
1
[OT] GNU bc base conversion
...t;interprets input.
>> >
> Thanks Gordon.
> Big bummer given that behavior. :-/
I suppose that depends on what you're trying to accomplish. Most
conversions you can do entirely within bash, if that's your goal.
function dec_to_hex () { printf '%x\n' $1; }
function hex_to_dec() { printf '%d\n' $(( 16#$1 )); }
function hex_to_bin() { echo 'obase=2;' $(( 16#$1 )) | bc; }
$ dec_to_hex 10
a
$ hex_to_dec a
10
$ hex_to_bin a
1010
2015 Aug 28
2
[OT] GNU bc base conversion
...e.mik.ua/orelly/unix/upt/ch49_03.htm
See below for my examples.
Thanks!
]$ echo "bin_to_dec(1001)" | bc
1001
# should be decimal 9
]$ echo "obase=10; ibase=2; 1001" | bc
9
]$ grep bin_to_dec ~/.bcrc
define bin_to_dec(b) { obase=10; ibase=2; return b; }
----
]$ echo "hex_to_dec(AB)" | bc
99
# should be decimal 171
]$ echo "obase=10; ibase=16; AB" | bc
171
]$ echo "obase=A; ibase=16; AB" | bc
171
]$ grep hex_to_dec ~/.bcrc
define hex_to_dec(h) { obase=A; ibase=16; return h; }
--
---~~.~~---
Mike
// SilverTip257 //
2015 Aug 28
3
[OT] GNU bc base conversion
On Aug 28, 2015, at 9:50 AM, Gordon Messmer <gordon.messmer at gmail.com> wrote:
>
> On 08/28/2015 07:15 AM, Mike - st257 wrote:
>> Thoughts as to why my BC functions aren't properly converting between bases?
>>
>> Decimal to binary or hex works fine, but not binary or hex to decimal and
>> so forth.
>
> I'm not an expert in bc, so I might be