开发者

Hexadecimal to Hexatridecimal in c++

I am attempting to 开发者_如何学Goconvert base 16 to base 36. I'm taking md5 hashes and making them have all 0-9a-z.

I searched around and didn't find anything good. Any suggestions for converting hexadecimal to hexatridecimal in c++? Do you guys know any good libraries for doing it?


I assume the tricky part you're struggling with is the conversion to Rad36, not getting a integral value from a hex number represented as a string. So, here is a function which takes an unsigned __int64, converts it to Radix 36, and returns a string with the converted value.

string rad36(unsigned __int64 v)
{
    string retval;
    while( v > 0 )
    {
        unsigned m = v%36;
        if( m <= 9 )
            retval.insert(0,1,'0'+m);
        else
            retval.insert(0,1,'A'+m-10);
        v /= 36;
    }
    return retval;
}


The basic solution is to convert your 128-bit number to a (large) integer, then subsequently perform modulus and divide operations by 36.

However, if you're OK with wasting a couple of bits, why not convert to base 32 to make things easier? Then you can do everything with shifting and masking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜