Search code examples
shellshradix

shell: arithmetic with custom number bases


I have to write a command line that take two numbers in two different custom base and output in a third one.

By custom base, I mean that I have things like that:

  • Input base 1: "!?\
  • Input base 2: ajedpoi
  • Output: rAfeB oiX

So starting from that I guess that bc doesn't help, right?

Is there such a command or should I write some custom script? Could you give me some clues? I'm completly lost.

EDIT: the exercice exact instructions are:

Write a command line that takes numbers from variables FT_NBR1, in ’"?! base, and FT_NBR2, in mrdoc base, and displays the sum of both in gtaio luSnemf base.

In a way that with the following values:

FT_NBR1=\'?"\"'\
FT_NBR2=rcrdmddd

I would get the following output:

Salut

Solution

  • Well, I was'nt clever enough. I can do it with just tr and bc

    I use tr to convert from and back the weird "bases" and bc with normal numbers.

    Here's what I have done:

    #!/bin/sh
    NUM1=$(echo "$FT_NBR1" | tr \'\\\\\"\?\! '01234')
    NUM2=$(echo "$FT_NBR2" | tr mrdoc '01234')
    RES=$(echo "obase=13; ibase=5; $NUM1 + $NUM2" | bc)
    
    echo $RES | tr '0123456789abc' 'gtaio luSnemf'