944,026 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 2875
  • C++ RSS
0

Conversion to any base, with a fixed number of digits

by on Oct 31st, 2009
Here's a function that manually converts an integer to any base between 2 and 36, the parameter list is:
C++ Syntax (Toggle Plain Text)
  1. void toBase(
  2. int value, // Integer value to convert
  3. char *target, // Pointer to a large enough buffer
  4. int base, // Base (from 2 to 36)
  5. int fixedDigitCount // The minimum number of digits it must contain
  6. );

This gives me the following output:
Quote ...
Binary with no fixed number of digits (1 to 15):
1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

Binary with 4 fixed digits (1 to 15):
0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111


Octal with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 10 11 12 13 14 15 16 17

Octal with 3 fixed digits (1 to 15):
001 002 003 004 005 006 007 010 011 012 013 014 015 016 017


Decimal with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Decimal with 4 fixed digits (1 to 15):
0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015


Hex with no fixed number of digits (1 to 15):
1 2 3 4 5 6 7 8 9 A B C D E F

Hex with 2 fixed digits (1 to 15):
01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Last edited by William Hemsworth; Oct 31st, 2009 at 10:24 am.
C++ Code Snippet (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const char *valLookup = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6. void toBase(int value, char *target, int base, int fixedDigitCount = 0) {
  7. int digitCount = 1;
  8. int temp = value;
  9. bool _signed = value < 0;
  10.  
  11. // Find digit count
  12. while ( temp /= base )
  13. digitCount++;
  14.  
  15. // Compare with fixeed number of digits, use highest
  16. int i = max( fixedDigitCount, digitCount ) + _signed;
  17.  
  18. // Add sign
  19. if ( _signed )
  20. target[0] = '-';
  21.  
  22. // Convert
  23. for (int off; i - _signed; i--) {
  24. off = (value % base);
  25. value /= base;
  26. target[i-1] = valLookup[off < 0 ? -off : off];
  27. }
  28.  
  29. // Add null-ptr
  30. target[ max(fixedDigitCount, digitCount) + _signed ] = 0;
  31. }
  32.  
  33. int main() {
  34. char buffer[32];
  35.  
  36. cout << "Binary with no fixed number of digits (1 to 15):\n";
  37. for (int i = 1; i <= 15; ++i) {
  38. toBase( i, buffer, 2 );
  39. cout << buffer << ' ';
  40. }
  41.  
  42. cout << "\n\nBinary with 4 fixed digits (1 to 15):\n";
  43. for (int i = 1; i <= 15; ++i) {
  44. toBase( i, buffer, 2, 4 );
  45. cout << buffer << ' ';
  46. }
  47.  
  48.  
  49. cout << "\n\n\nOctal with no fixed number of digits (1 to 15):\n";
  50. for (int i = 1; i <= 15; ++i) {
  51. toBase( i, buffer, 8 );
  52. cout << buffer << ' ';
  53. }
  54.  
  55. cout << "\n\nOctal with 3 fixed digits (1 to 15):\n";
  56. for (int i = 1; i <= 15; ++i) {
  57. toBase( i, buffer, 8, 3 );
  58. cout << buffer << ' ';
  59. }
  60.  
  61.  
  62. cout << "\n\n\nDecimal with no fixed number of digits (1 to 15):\n";
  63. for (int i = 1; i <= 15; ++i) {
  64. toBase( i, buffer, 10 );
  65. cout << buffer << ' ';
  66. }
  67.  
  68. cout << "\n\nDecimal with 4 fixed digits (1 to 15):\n";
  69. for (int i = 1; i <= 15; ++i) {
  70. toBase( i, buffer, 10, 4 );
  71. cout << buffer << ' ';
  72. }
  73.  
  74. cout << "\n\n\nHex with no fixed number of digits (1 to 15):\n";
  75. for (int i = 1; i <= 15; ++i) {
  76. toBase( i, buffer, 16 );
  77. cout << buffer << ' ';
  78. }
  79.  
  80. cout << "\n\nHex with 2 fixed digits (1 to 15):\n";
  81. for (int i = 1; i <= 15; ++i) {
  82. toBase( i, buffer, 16, 2 );
  83. cout << buffer << ' ';
  84. }
  85.  
  86. cin.ignore();
  87. }
Comments on this Code Snippet
Oct 31st, 2009
0

Re: Conversion to any base, with a fixed number of digits

Looks good.
Have you considered negative bases, for fullness?
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 31st, 2009
0

Re: Conversion to any base, with a fixed number of digits

I had never even heard of a negative base until you said that, I looked it up and it seems interesting. But, this function already uses a sign to represent negative numbers, maybe I could make a different one to handle negative bases.

Thanks for the feedback
Last edited by William Hemsworth; Oct 31st, 2009 at 10:57 am.
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Message:
Previous Thread in C++ Forum Timeline: Alittle Help for this Noob?
Next Thread in C++ Forum Timeline: Computing Average Test Scores





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC