Base Conversion: Base 2 to Base N Directly

Status
Not open for further replies.

Sevion

The DIY Ninja
Reaction score
413
Does anyone have any ideas on how to convert directly from Base 2 to Base N?

I.E.

0001 0010 1101 -> 12D

NOT:

0001 0010 1101 -> 301 -> 12D
 

Sevion

The DIY Ninja
Reaction score
413
Sorry. The language I want to make it in is C++.

Afterwards, I'm thinking of porting it to vJASS.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Why bother?

What's so bad about of string hexy = dec2hex(bin2dec("000100101101"))?

Edit:
About 2 -> 16 directly:

JASS:
#include <string>

#include <iostream>

using std::string;
using std::cout;
using std::cin;
using std::endl;

string substr(string& s, int start, int end)
{
    return string(s, start, end - start);
}

char hex_digit(int n)
{
    char d;
    switch (n)
    {
    case 0: d = '0'; break;
    case 1: d = '1'; break;
    case 2: d = '2'; break;
    case 3: d = '3'; break;
    case 4: d = '4'; break;
    case 5: d = '5'; break;
    case 6: d = '6'; break;
    case 7: d = '7'; break;
    case 8: d = '8'; break;
    case 9: d = '9'; break;
    case 10: d = 'A'; break;
    case 11: d = 'B'; break;
    case 12: d = 'C'; break;
    case 13: d = 'D'; break;
    case 14: d = 'E'; break;
    case 15: d = 'F'; break;
    default: d = 'X';
    }

    return d;
}

string bin2hex(const string& b)
{
    string result = "";
    int sum = 0;
    int power = 1;
    int i;
    int c = 0;

    for (i = (int)b.length() - 1; i >= 0; i--)
    {
        if (b<i> == &#039;1&#039;)
        {
            sum += power;
        }        
        
        c++;
        power *= 2;        
        
        if (c == 4)
        {
            result = hex_digit(sum) + result;
            sum = 0;
            c = 0;
            power = 1;
        }
    }

    if (c != 0)
    {
        result = hex_digit(sum) + result;
    }

    return result;
}
    
int main()
{
    string s = &quot;&quot;;
   
    while (s != &quot;exit&quot;)
    {
        cin &gt;&gt; s;
        cout &lt;&lt; bin2hex(s) &lt;&lt; endl;
    }

    return 0;
}
</i>


I guess the jass version would be the same except it won't be b == '1' but substr(b, i, i + 1) == "1"
and ofc hex_digit would take more screen space and all 'X' would be "X"
 

Sevion

The DIY Ninja
Reaction score
413
I think you completely misunderstand the question.

This isn't a question about Base 2 -> Base 16. It's a question about Base 2 -> Base N.
 

Sevion

The DIY Ninja
Reaction score
413
Any characters.

I.E. Base 2 -> Base 8 should work, but so should Base 2 -> Base 16 and Base 2 -> Base 85 or Base 64 or Base 32. Etc. I want the algorithm to work for Base 2 -> Base N.
 

celerisk

When Zerg floweth, life is good
Reaction score
62
Well... multiply number by 2, add current bit. Repeat for all bits from most- to least-significant.
Surprising as it may be, it works just the same for all bases.

Example:
11001(2) to base 10:

Start at 0.
1: times 2 + 1 -> 1
1: times 2 + 1 -> 3
0: times 2 + 0 -> 6
0: times 2 + 0 -> 12
1: times 2 + 1 -> 25


Same number to base 5:


Start at 0.
1: times 2 + 1 -> 1
1: times 2 + 1 -> 3
0: times 2 + 0 -> 11 (6 > 5: overflow... 1 * 5 + 1)
0: times 2 + 0 -> 22
1: times 2 + 1 -> 100 (22 * 2 + 1 -> 45 -> (4 + 1) 0 -> 50 -> 100)

Test: 1 * 25 + 0 * 5 + 0 = 25, seems correct.


Code:
#include <stdio.h>

int show(int* result, int max)
{
        int i;
        char* thingy = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./ :) !!11!WTF!LOL!!HereBeDragons!";
        for (i = max; i >= 0; printf("%c", thingy[result[i--]]));
        return putchar('\n');
}

int bin2base(char* bits, int base)
{
        int i,max = 0;
        int result[100] = {0};

        if (base < 2)
        {
                return printf("Nice try\n");
        }

        for (; *bits; bits++)
        {
                for (i = 0; i <= max; i++)
                {
                        result[i] <<= 1;
                }
                result[0] += (*bits == '1' ? 1 : 0);
                for (i = 0; i <= max; i++)
                {
                        if (result[i] >= base)
                        {
                                result[i] -= base;
                                if (i == max)
                                {
                                        result[++max] = 0;
                                }
                                result[i + 1]++;
                        }
                }
        }
        return show(result, max);
}

int main(int argc, char** argv)
{
        return bin2base("11001", 5);
}
 

Sevion

The DIY Ninja
Reaction score
413
Wow. Really nice. Only way it could have been better is if you had told me how to do it and I did it myself :p I seem to learn better that way XD

But now I can dissect the code. Thanks.
 
Status
Not open for further replies.
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top