你如何转换成字符数为十进制和背部或转换ASCII'A'

编程入门 行业动态 更新时间:2024-10-17 23:34:48
本文介绍了你如何转换成字符数为十进制和背部或转换ASCII'A' - 'Z'/'A' - 'Z',以信偏移0'A'/'A'...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果你有一个char是在'0'到'9'你怎么把它转换为int的0值9

If you have a char that is in the range '0' to '9' how do you convert it to int values of 0 to 9

和那你怎么转换回来?

还给出字母'A'到'Z'或'a'到'Z'你怎么把它们转换成0-25的范围,然后回来?

Also given letters 'A' to 'Z' or 'a' to 'z' how do you convert them to the range 0-25 and then back?

这是好以优化ASCII

It is okay to optimize for ASCII

推荐答案

用C ++指定的基本字符编码,使转换和从'0' - '9'容易

The basic char encoding specified by C++ makes converting to and from '0' - '9' easy.

C ++规定:

在源和执行的基本字符集,每个字符的十进制数字上面的列表0后的值应大于previous的大1的值。

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

这意味着,不论0的积分值,'1'的积分值 0 + 1 的积分值'2'为 0 + 2 ,等等。利用这些信息和算术的基本规则,你可以从字符转换成int和背部容易:

This means that, whatever the integral value of '0', the integral value of '1' is '0' + 1, the integral value of '2' is '0' + 2, and so on. Using this information and the basic rules of arithmetic you can convert from char to int and back easily:

char c = ...; // some value in the range '0' - '9' int int_value = c - '0'; // int_value is in the range 0 - 9 char c2 = '0' + int_value;

可移植转换的字母A到Z为数字从0到25是不容易,因为C ++不指定这些字母的数值是连续的。在ASCII他们是连续的,你可以写code,它依赖于类似上述code为'0' - '9'。 (这些天ASCII使用最无处不在)。

Portably converting the letters 'a' to 'z' to numbers from 0 to 25 is not as easy because C++ does not specify that the values of these letters are consecutive. In ASCII they are consecutive, and you can write code that relies on that similar to the above code for '0' - '9'. (These days ASCII is used most everywhere).

便携式code将改用一个查找表或为每个字符具体检查:

Portable code would instead use a lookup table or a specific checks for each character:

char int_to_char[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int char_to_int[CHAR_MAX + 1] = {}; for (int i=0; i<sizeof(int_to_char); ++i) { char_to_int[int_to_char[i]] = i; } // convert a lowercase char letter to a number in the range 0 - 25: int i = char_to_int['d']; // convert an int in the range 0 - 25 to a char char c = int_to_char[25];

在C99你可以直接初始化 char_to_int [] 数据没有一个循环。

In C99 you can just directly initialize the char_to_int[] data without a loop.

int char_to_int[] = {['a'] = 0, ['b'] = 1, ['c'] = 2, ['d'] = 3, ['e'] = 4, ['f'] = 5, ['g'] = 6, ['h'] = 7, ['i'] = 8, ['j'] = 9, ['k'] = 10, ['l'] = 11, ['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15, ['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19, ['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23, ['y'] = 24, ['z'] = 25};

C ++编译器还支持C99可以支持这个在C ++中为好,作为扩展。

C++ compilers that also support C99 may support this in C++ as well, as an extension.

下面是生成随机值在这些转换使用一个完整的程序。它使用C ++,加上C99指定初始化扩展。

Here's a complete program that generates random values to use in these conversions. It uses C++, plus the C99 designated initialization extension.

#include <cassert> int digit_char_to_int(char c) { assert('0' <= c && c <= '9'); return c - '0'; } char int_to_digit_char(int i) { assert(0 <= i && i <= 9); return '0' + i; } int alpha_char_to_int(char c) { static constexpr int char_to_int[] = {['a'] = 0, ['b'] = 1, ['c'] = 2, ['d'] = 3, ['e'] = 4, ['f'] = 5, ['g'] = 6, ['h'] = 7, ['i'] = 8, ['j'] = 9, ['k'] = 10, ['l'] = 11, ['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15, ['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19, ['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23, ['y'] = 24, ['z'] = 25}; assert(0 <= c && c <= sizeof(char_to_int)/sizeof(*char_to_int)); int i = char_to_int[c]; assert(i != 0 || c == 'a'); return i; } char int_to_alpha_char(int i) { static constexpr char int_to_char[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; assert(0 <= i && i <= 25); return int_to_char[i]; } #include <random> #include <iostream> int main() { std::random_device r; std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()}; std::mt19937 m(seed); std::uniform_int_distribution<int> digits{0, 9}; std::uniform_int_distribution<int> letters{0, 25}; for (int i=0; i<20; ++i) { int a = digits(m); char b = int_to_digit_char(a); int c = digit_char_to_int(b); std::cout << a << " -> '" << b << "' -> " << c << '\n'; } for (int i=0; i<20; ++i) { int a = letters(m); char b = int_to_alpha_char(a); int c = alpha_char_to_int(b); std::cout << a << " -> '" << b << "' -> " << c << '\n'; } }

更多推荐

你如何转换成字符数为十进制和背部或转换ASCII'A'

本文发布于:2023-10-24 20:08:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背部   转换成   数为   字符   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!