Convert hex-> dec - looking for beautiful solutions

    After discussing the algorithms for finding numbers in a string , an idea arose to consider some other tasks in the same vein. The task of converting an integer from a hexadecimal system to a decimal system seemed to be suitable: not very cumbersome, with at least one obvious algorithm and, perhaps, many non-obvious ones, and understandable.

    Given a string containing a non-negative integer in a hexadecimal system. The number is written in digits 0..9A..F (letters are only large), it is guaranteed that there are no non-digits in the line. You need to get a string containing a decimal notation of such a number. The length of the input string does not exceed 100,000 bytes.

    Write solutions in any languages ​​- the most beautiful, shortest, most effective ... For solutions on a Turing machine, I suggest using only the characters "space, 0, 1" (do not enter new ones), and write the input and output numbers with four bits (separated by spaces or not) - at your discretion). On Brainfuck, the input string should be entered, and the output should be printed.

    UPD Using the built-in or library BigNum is, of course, good, but somehow not very interesting. Let's do without it!

    For the seed - a solution in C # (which can not be called neither beautiful, nor short, nor effective):

            static unsafe string Hex2Dec (string x) {
                int l = x.Length;
                int ll = l + l / 4 + 3;
                sbyte [] m = new sbyte [ll];
                int i = l;
                foreach (char c in x) m [- i] = (sbyte) (c <'A'? c-'0 ': c-'A' + 10);
                int lk = ll;
                while (l> 0) {
                    int k = 0, l1 = 0;
                    while (l> 0) {
                        k = (k << 4) + m [- l];
                        m [l] = (sbyte) (k / 10);
                        if (l1 == 0 && k> = 10) l1 = l + 1;
                        k% = 10;
                    m [- lk] = (sbyte) (k + 48);
                    l is l1;
                string res;
                fixed (sbyte * c = m) {res = new string (c, lk, ll-lk); }
                return res;

    Also popular now: