c++ 将数字转换为字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9259140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting numbers into alphabets in c++
提问by Bhabes
I'm trying to write a code that would convert numbers into alphabets. For example 1 will be 'A', 2 will be 'B', 3 will be 'C' and so on. Im thinking of writing 26 if statements. I'm wondering if there's a better way to do this...
我正在尝试编写一个将数字转换为字母的代码。例如,1 将是“A”,2 将是“B”,3 将是“C”,依此类推。我正在考虑编写 26 个 if 语句。我想知道是否有更好的方法来做到这一点......
Thank you!
谢谢!
回答by Fred Foo
Use an array of letters.
使用字母数组。
char nth_letter(int n)
{
assert(n >= 1 && n <= 26)
return "abcdefghijklmnopqrstuvwxyz"[n-1];
}
回答by CashCow
If you can rely on the using an ASCII character set where they are consecutive then you can convert
如果您可以依靠使用连续的 ASCII 字符集,那么您可以转换
char intToAlphabet( int i )
{
return static_cast<char>('A' - 1 + i);
}
If you can sometimes rely on this fact, e.g. you can set a compiler flag or similar for the particular target, you can also use this code for that specific build.
如果您有时可以依赖这一事实,例如您可以为特定目标设置编译器标志或类似标志,您也可以将此代码用于该特定构建。
Otherwise use a static lookup table (as others have suggested).
否则使用静态查找表(正如其他人所建议的那样)。
Note that it is preferable to "assert" your range check if your numbered input comes from program variables that you know should never be out of range.
请注意,如果您的编号输入来自您知道不应超出范围的程序变量,则最好“断言”您的范围检查。
If the input comes from user-provided data where the users could potentially provide rogue data, you need a way to handle it that is not "undefined behaviour". Therefore you would have to check each value and either throw an exception (informing the user to correct their data) or use some character to indicate a bad input.
如果输入来自用户提供的数据,而用户可能会在其中提供恶意数据,则您需要一种非“未定义行为”的处理方式。因此,您必须检查每个值并抛出异常(通知用户更正其数据)或使用某些字符来指示错误输入。
回答by James Kanze
The simplest way would be using a table:
最简单的方法是使用表格:
char
remap( int original )
{
static char const remap[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return original >= 0 && original < sizeof(remap) - 1
? remap[ original ]
: '?'; // or throw, or ...
}
回答by Maxim Kholyavkin
something like that
类似的东西
my_print(int x)
{
char symbol = (char)('A' + x - 1);
cout << symbol;
}
回答by lingjiankong
If you want to convert 0
to a
, 1
to b
, 2
to c
... 25
to z
:
如果您想转换0
为a
、1
到b
、2
到c
...25
到z
:
char covertedChar = (char)('a' + 2);
// Will print 'c'
cout << covertedChar;
Similary, to convert 0
to A
, 1
to B
, 2
to C
... 25
to Z
:
类似地,要转换0
为A
、1
到B
、2
到C
...25
到Z
:
char covertedChar = (char)('A' + 25);
// Will print 'Z'
cout << covertedChar;
So if you want to convert 1
to A
, 2
to B
, 3
to C
... 26
to Z
, simply subtract 1
offset.
所以如果你想转换1
为A
, 2
to B
, 3
to C
... 26
to Z
,只需减去1
偏移量。
int toCovert = 26 // 'Z'
char covertedChar = (char)('A' + toCovert - 1);
// Will print 'Z'
cout << covertedChar;