Efficiently convert between Hex, Binary, and Decimal in C/C++ -


I have 3 base presentations for positive integer numbers:

  1. Decimal, unsigned long In the variable (for example unsigned long-numbered NumDec = 200 )
  2. In hex, string variable (like string NumHex = "C8" )
  3. in binary, string variable (for example string NumBin = "11001000" )

I want to be able to convert between all the 3 representatives in most effective way. To wit. To implement the following 6 function:

  Unsigned long intervals Binary2Dec (const string and bin) {} unsigned long end hex 2 dcc string (string and hex) string dec2hex (unsigned long int dec) {} String Binary2Hex (const string and bin) {} String Dec2Binary (unsigned long int dec) {} String Hex2Binary (const string and hex) {}  

What is most of them Efficient approach to? I can use C and C ++, but do not promote

Edit: By means of "efficiency" I mean time efficiency: shortest execution time

As others have said, I will start with it, and / or for most applications Fast is fast, and they are less likely to have a bug, I would say, however, these functions are more common than you expected, because they have to deal with non-ASCII character sets, with numbers displayed on any base and even further. For some domains, this library can beat the functions.

Then, first take measures, and if these conversions are really an issue then:

1) In some applications / domains, some numbers appear very often, for example Zero, 100, 200, 19.95, can be so common that by doing so, it is understandable to convert such numbers with () statement if, and then generic library work 2) the most common 100 numbers if you use a table lookup, and then a library Go back to Hnkshn. Remember that large tables can not be fitted in your cache and may require multiple interactions for the shared library, so be sure to minimize the performance carefully to measure these things carefully.

You have a Lexical_cast function, however, in my experience the latter is compared to the relatively good old C forms.

A lot of people say this, it's repeatable: do not optimize these conversions unless you have proof that they are a problem. If you are optimizing, to ensure that it is fast and measure your new implementation, you have a unit test for your own version, because you Worms will: - (


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -