从52进制到十进制,反之亦然

本文关键字:十进制 反之亦然 | 更新日期: 2023-09-27 18:15:20

我正试图调整这段代码,可以执行转换和从Base 52,我用它来存储RGB颜色信息从c#到c++:

public static string ColourToBase52(Color colour)
        {
            int value = colour.ToArgb() & 0x00FFFFFF; // Mask off the alpha channel.
            return ToBase52(value);
        }
        public static Color ColourFromBase52(string colour)
        {
            int value = FromBase52(colour);
            return Color.FromArgb(unchecked((int)(0xFF000000 | value)));
        }
        public static string ToBase52(int value)
        {
            char[] baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int targetBase = baseChars.Length;
            int i = 32;
            char[] buffer = new char[i];
            do
            {
                buffer[--i] = baseChars[value % targetBase];
                value = value / targetBase;
            }
            while (value > 0);
            char[] result = new char[32 - i];
            Array.Copy(buffer, i, result, 0, 32 - i);
            return new string(result).PadLeft(5, 'a');
        }
        public static int FromBase52(string value)
        {
            char[] baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int targetbase = baseChars.Length;
            int multiplier = 1;
            int result = 0;
            for (int i = value.Length-1; i >= 0; --i)
            {
                int digit = Array.IndexOf(baseChars, value[i]);
                result += digit*multiplier;
                multiplier *= targetbase;
            }
            return result;
        }

对于我的c++代码,我选择将获取和返回颜色值为整数的函数与Base 52转换函数结合起来:

struct DIFColor *DIFBase52ToColor(std::string c)
{
    const char *baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int targetBase = 52;
    int multiplier = 1;
    int result = 0;
    const char *d = c.c_str();
    for (int i = c.length() - 1; i >= 0; --i)
    {
        int digit = DIFGetPositionInArray(baseChars, sizeof(baseChars), c[i]);
        result += digit * multiplier;
        multiplier = multiplier * targetBase;
    }

    uint8_t b = result & 255;
    uint8_t g = (result >> 8) & 255;
    uint8_t r = (result >> 16) * 255;
    return CreateDIFColor(r,g,b);
}
std::string DIFColorToBase52(struct DIFColor *c)
{
    int rgb = ((c->r&0x0ff)<<16)|((c->g&0x0ff)<<8)|(c->b&0x0ff);
    const char *baseChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int targetBase = 52;
    int i = 32;
    char *buffer = new char[i];
    do
    {
         buffer[--i] = baseChars[rgb % targetBase];
         rgb = rgb / targetBase;
    }
    while (rgb > 0);
    char *result = new char[32 - i];
    DIFCopyCharArray((const char *)buffer, i, 0, 32 - i, result);
    std::string s((const char*)result);
    s.insert(s.begin(), 5 - s.size(), 'a');
    return s;
}

我还必须为数组操作创建两个函数:

int DIFGetPositionInArray(const char *array, size_t size, const char c)
{
    for (size_t i = 0; i < size; i++)
    {
                if (array[i] == c)
                    return (int)i;
    }
    return -1;
}
void DIFCopyCharArray(const char* source, int wheretostart, int wheretocopy, int numtocopy, char *dest)
{
    int c = wheretocopy;

    for(int i = wheretostart; i <= numtocopy; i++)
    {
        dest[c] = source[i];
        c++;
    }

}

然而,当我试图用完整性检查测试它时,它失败了:

255,255,255 = 'aah1U' in Base52 RGB
aah1U = 1,245,59 in RGB

似乎每次运行完整性检查时,都会产生一个不同的值:

255,255,255 = 'aah13' in Base52 RGB
aah13 = 1,245,59 in RGB
255,255,255 = 'aah1j' in Base52 RGB
aah1j = 1,245,59 in RGB

预期输出为:

255,255,255 = 'cpqEN' in Base52 RGB
cpqEN = 255,255,255 in RGB

让我认为这可能是一个指针的问题

从52进制到十进制,反之亦然

错误可能是您没有在任何地方终止result字符串,这会导致未定义的行为在以下情况:

std::string s((const char*)result);

这是因为std::string构造函数在复制你传递给它的c风格字符串时寻找终止符。

您可以通过两种方式解决:将终止符''0'添加到result,或者告诉std::string构造器result的长度。

问题在于数组复制函数不正确。应该是:

void DIFCopyCharArray(const char* source, int wheretostart, int wheretocopy, int numtocopy, char *dest)
{
    int c = wheretocopy;

    for(int i = wheretostart; c <= numtocopy; i++)
    {
        dest[c] = source[i];
        c++;
    }
    dest[c] = ''0';

}

同样,数组搜索函数不能工作,因为sizeof(baseChars)返回4,这不是元素的数目。

使用如下函数:

int DIFGetPositionInArray(const char *array, int arrayElements, const char c)
{
    for (int i = 0; i < arrayElements; i++)
    {
                if (array[i] == c)
                    return i;
    }
    return -1;
}

这样命名;

DIFGetPositionInArray(baseChars,52,d[i]);