在 C/C++ 中高效地在十六进制、二进制和十进制之间进行转换
我有 3 种正整数的基本表示:
I have 3 base representations for positive integer numbers:
- 十进制,在 unsigned long 变量中(例如 unsigned long int NumDec = 200).
- 十六进制,在字符串变量中(例如 string NumHex = "C8")
- 二进制,在字符串变量中(例如string NumBin = "11001000")
我希望能够以最有效的方式在所有 3 种表示中的数字之间进行转换.IE.实现以下6个功能:
I want to be able to convert between numbers in all 3 representations in the most efficient way. I.e. to implement the following 6 functions:
unsigned long int Binary2Dec(const string & Bin) {}
unsigned long int Hex2Dec(const string & Hex) {}
string Dec2Hex(unsigned long int Dec) {}
string Binary2Hex(const string & Bin) {}
string Dec2Binary(unsigned long int Dec) {}
string Hex2Binary(const string & Hex) {}
对他们每个人来说最有效的方法是什么?我可以使用 C 和 C++,但不能使用 boost.
What is the most efficient approach for each of them? I can use C and C++, but not boost.
效率"是指时间效率:最短的执行时间.
By "efficiency" I mean time efficiency: Shortest execution time.
推荐答案
正如其他人指出的,我会从 sscanf()
, printf()
和/或 strtoul()
.它们对于大多数应用程序来说足够快,而且它们不太可能出现错误.然而,我要说的是,这些函数比你想象的更通用,因为它们必须处理非 ASCII 字符集,以及以任何基数表示的数字等等.对于某些领域,有可能击败库函数.
As others have pointed out, I would start with sscanf()
, printf()
and/or strtoul()
. They are fast enough for most applications, and they are less likely to have bugs. I will say, however, that these functions are more generic than you might expect, as they have to deal with non-ASCII character sets, with numbers represented in any base and so forth. For some domains it is possible to beat the library functions.
所以,先测量,如果这些转换的性能确实是个问题,那么:
So, measure first, and if the performance of these conversion is really an issue, then:
1) 在某些应用程序/域中,某些数字经常出现,例如零、100、200、19.95,可能非常常见,因此优化您的函数以使用一堆 if() 语句转换这些数字是有意义的,然后回退到通用库函数.2)如果最常见的 100 个数字使用表查找,然后回退到库函数.请记住,大型表可能不适合您的缓存,并且可能需要对共享库进行多次间接访问,因此请仔细测量这些内容以确保不会降低性能.
1) In some applications / domains certain numbers appear very often, for example zero, 100, 200, 19.95, may be so common that it makes sense to optimize your functions to convert such numbers with a bunch of if() statements, and then fall back to the generic library functions. 2) Use a table lookup if the most common 100 numbers, and then fall back on a library function. Remember that large tables may not fit in your cache and may require multiple indirections for shared libraries, so measure these things carefully to make sure you are not decreasing performance.
您可能还想查看 boost lexical_cast 函数,尽管根据我的经验,后者与良好的旧 C 函数相比.
You may also want to look at boost lexical_cast functions, though in my experience the latter are relatively compared to the good old C functions.
尽管很多人都说过,但值得一遍又一遍地重复:在您有证据证明它们存在问题之前,不要优化这些转化.如果您确实进行了优化,请测量您的新实现以确保它更快并且确保您为自己的版本进行大量单元测试,因为您会引入错误:-(
Tough many have said it, it is worth repeating over and over: do not optimize these conversions until you have evidence that they are a problem. If you do optimize, measure your new implementation to make sure it is faster and make sure you have a ton of unit tests for your own version, because you will introduce bugs :-(
相关文章