基本转换问题 [英] Base Conversion Problem

查看:241
本文介绍了基本转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个整数转换为字符串,我现在有一个问题。



我已经得到了代码编写和工作大部分,但它有一个小缺陷,携带到下一个地方。这很难描述,所以我给你一个例子。使用由小写字母组成的字符集的基数26:



0 =a

1 =b

2 =c



...



25 =z

26 =ba(这应该等于aa)



在某些情况下,似乎在字符集的零位置跳过该字符。 p>

令我困惑的是,我的代码看不出什么问题。

  char * charset = (char *)abcdefghijklmnopqrstuvwxyz; 
int charsetLength = strlen(charset);

unsigned long long num = 5678; //一些随机数,无所谓
std :: string key

do
{
unsigned int remainder =(num%charsetLength);
num / = charsetLength;

key.insert(key.begin(),charset [remainder]);

} while(num);

我有一个感觉函数是跳过模数返回零,工作这么久,我不知道它是如何发生的。欢迎任何建议。



编辑:生成的字符串是小字节序的事实与我的应用程序无关。

解决方案

如果我正确理解你想要什么(excel用于列,A,B,... Z,AA,AB,...的编号),这是一个基于符号以表示从1开始的数字.26个数字具有值1,2,... 26并且基数为26.因此A具有值1,Z值26,AA值27 ...计算此表示非常类似于正常回报,你只需要调整偏移量1而不是0。

  #include< string& 
#include< iostream>
#include< climits>

std :: string base26(unsigned long v)
{
char const digits [] =ABCDEFGHIJKLMNOPQRSTUVWXYZ;
size_t const base = sizeof(digits) - 1;
char result [sizeof(unsigned long)* CHAR_BIT + 1];
char * current = result + sizeof(result);
* - current ='\0';

while(v!= 0){
v--;
* - current = digits [v%base];
v / = base;
}
return current;
}

//用于测试
#include< cstdlib>

int main(int argc,char * argv [])
{
for(int i = 1; i unsigned long value = std :: strtol(argv [i],0,0);
std :: cout<<值<< =<< base26(value)<< '\\\
';
}
return 0;
}

运行1 2 26 27 52 53 676 677 702 703给出

  1 = A 
2 = B
26 = Z
27 = AA
52 = AZ
53 = BA
676 = YZ
677 = ZA
702 = ZZ
703 = AAA
pre>

I'm trying to convert an integer to a string right now, and I'm having a problem.

I've gotten the code written and working for the most part, but it has a small flaw when carrying to the next place. It's hard to describe, so I'll give you an example. Using base 26 with a character set consisting of the lowercase alphabet:

0 = "a"
1 = "b"
2 = "c"

...

25 = "z"
26 = "ba" (This should equal "aa")

It seems to skip the character at the zero place in the character set in certain situations.

The thing that's confusing me is I see nothing wrong with my code. I've been working on this for too long now, and I still can't figure it out.

char* charset = (char*)"abcdefghijklmnopqrstuvwxyz";
int charsetLength = strlen(charset);

unsigned long long num = 5678; // Some random number, it doesn't matter
std::string key

do
{
    unsigned int remainder = (num % charsetLength);
    num /= charsetLength;

    key.insert(key.begin(), charset[remainder]);

} while(num);

I have a feeling the function is tripping up over the modulo returning a zero, but I've been working on this so long, I can't figure out how it's happening. Any suggestions are welcome.

EDIT: The fact that the generated string is little endian is irrelevant for my application.

解决方案

If I understand correctly what you want (the numbering used by excel for columns, A, B, .. Z, AA, AB, ...) this is a based notation able to represent numbers starting from 1. The 26 digits have values 1, 2, ... 26 and the base is 26. So A has value 1, Z value 26, AA value 27... Computing this representation is very similar to the normal reprentation you just need to adjust for the offset of 1 instead of 0.

#include <string>
#include <iostream>
#include <climits>

std::string base26(unsigned long v)
{
    char const digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    size_t const base = sizeof(digits) - 1;
    char result[sizeof(unsigned long)*CHAR_BIT + 1];
    char* current = result + sizeof(result);
    *--current = '\0';

    while (v != 0) {
        v--;
        *--current = digits[v % base];
        v /= base;
    }
    return current;
}

// for testing
#include <cstdlib>

int main(int argc, char* argv[])
{
    for (int i = 1; i < argc; ++i) {
        unsigned long value = std::strtol(argv[i], 0, 0);
        std::cout << value << " = " << base26(value) << '\n';
    }
    return 0;
}

Running with 1 2 26 27 52 53 676 677 702 703 gives

1 = A
2 = B
26 = Z
27 = AA
52 = AZ
53 = BA
676 = YZ
677 = ZA
702 = ZZ
703 = AAA

这篇关于基本转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆