简体   繁体   English

Codekata 将数字从十六进制转换为十进制整数表示

[英]Codekata to convert number from hex to dec integer representation

I am writing a small program convert hex representation of a string , it is a kata to improve my skills.我正在编写一个小程序转换字符串的十六进制表示,这是提高我的技能的一个kata。

This is what I have come up with这是我想出的

std::vector<int> decimal( std::string const & s )
{

auto getint = [](char const k){
switch(k){
case 'f':
return 15;
case 'e':
return 14;
case 'd':
return 13;
case 'c':
return 12;
case 'b':
return 11;
case 'a':
return 10;
case '9':
return 9;
case '8':
return 8;
case '7':
return 7;
case '6':
return 6;
case '5':
return 5;
case '4':
return 4;
case '3':
return 3;
case '2':
return 2;
case '1':
return 1;
case '0':
return 0;
};

std::vector<int> result;

for( auto const & k : s )
{
result.push_back(getint(k));
}

return result;

}

I was wondering if there in another way to do this.我想知道是否有另一种方式来做到这一点。 I have considered to use something as an std::map as well, but I am uncertain which one might be faster.我也考虑过使用一些东西作为 std::map ,但我不确定哪个可能更快。 If there is another way to do this please add it.如果有其他方法可以做到这一点,请添加它。

Please keep in mind that I am doing this as a code-kata to improve my skills, and learn.请记住,我这样做是为了提高我的技能和学习。

Thanks and TIA!谢谢和 TIA!

To start with, you can probably simplify your logic like so:首先,您可以像这样简化您的逻辑:

auto getint = [](char const k){
    if(k >= 'a' && k <= 'f') return (k - 'a');
    else if(k >= 'A' && k <= 'F') return (k - 'A');
    else if(k >= '0' && k <= '9') return (k - '0');
    else return -1;
}

Beyond that, there may exist a Standard Library function that does exactly this, which you might prefer depending on your specific needs.除此之外,可能存在完全执行此操作的标准库函数,您可能更喜欢根据您的特定需求。

You can use strtol or strtoll to do most of the heavy lifting for you of converting from a base16 string to an integer value.您可以使用strtol 或 strtoll为您完成从 base16 字符串转换为整数值的大部分繁重工作。

Then convert back to a regular string using a stringstream object.然后使用 stringstream 对象转换回常规字符串。

// parse hex string with strtol
long value = ::strtol(s.c_str(), nullptr, 16);  //not shown - checking for errors. Read the manual page for more info

// convert value back to base-10 string
std::stringstream st;
st << value;
std::string result = st.str();

return result;

For the decimal digits it's very easy to convert a character to its digit, as the C++ specification says that all digits must be consecutive in all encodings, with '0' being the lowest and '9' the highest.对于十进制数字,很容易将字符转换为它的数字,因为 C++ 规范规定所有数字在所有编码中都必须是连续的, '0'是最低的, '9'是最高的。 That means you could convert a character to number by just subtracting '0' , like eg k - '0' .这意味着您只需减去'0'即可将字符转换为数字,例如k - '0' There's no such requirement for the letters though, but the most common encoding (ASCII) the same is true, but it should not be counted on if you want to be portable.虽然对字母没有这样的要求,但最常见的编码 (ASCII) 也是如此,但如果您想便携,则不应指望它。

You could also do it using eg std::transform and std::back_inserter , so no need for your own loop.你也可以使用std::transformstd::back_inserter来做到这一点,所以不需要你自己的循环。 Perhaps something like也许像

std::transform(std::begin(s), std::end(s), std::back_inserter(result), getint);

In the getint function you could use eg std::isxdigit and std::isdigit to check if the character is a valid hexadecimal or decimal digit, respectively.getint函数中,您可以分别使用std::isxdigitstd::isdigit来检查字符是否为有效的十六进制或十进制数字。 You should probably be using eg std::tolower in case the hexadecimal digits are upper-case.您可能应该使用例如std::tolower以防十六进制数字为大写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM