Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,106 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,141 people online right now. Registration is fast and FREE... Join Now!





Alpha to Binary/Hexadecimal/Octal

Converts alpha characters to lots of useless stuff that might come in handy for someone..sometime. Also includes binary to alpha functions, sorry no hexadecimal to alpha yet

Submitted By: Dark_Nexus
Actions:
Rating:
Views: 15,914

Language: C++

Last Modified: August 29, 2008
Instructions: POW(int base, int power)
+Description:
Calculates exponents. Used within the header file, not intended for outside use.
+Parameters:
base^power
+Return:
Returns base^power in integer form

ATOB(char a, int* dest)
+Description:
Converts ASCII characters into binary in 8 bit form
+Parameters:
a = Character to be converted
dest = Head of an int/char/bool array (See overloads)
+Return:
No returned value
+Overloads:
ATOB(char a, char* dest)
ATOB(char a, bool* dest)

ATOH(char a, char* dest)
+Description:
Converts ASCII characters to hexadecimal form in 2 bit form
+Parameters:
a = Character to be converted
dest = Head of a char array
+Return:
No returned value

ATOO(char a, int* dest)
+Description:
Converts ASCII characters to octal in 4 bit form
+Parameters:
a = Character to be converted
dest = Head of an int/char array (See overloads)
+Return:
No returned value
+Overloads:
ATOO(char a, char* dest)

BTOA(int bin[8])
+Description:
Converts up to 8 bits of binary into an alpha-numerical character
+Parameters:
bin = an array of 8 int/bool (See overloads)
+Return:
Character derived from the passed binary string
+Overloads:
BTOA(bool bin[8])

OTOA(int oct[4])
+Description:
Converts octal into ASCII character
+Parameters:
oct = and array of 4 int
+Return:
Returns the derived ASCII character

NOTE: Does not support extended character sets; maximum decimal value is 127. Functions do not check validity of pointers; a pointer to an array below the recommended dimension may cause programs to malfunction.

Snippet


  1. int POW(int base, int power)
  2. {
  3.      if (power)
  4.      {
  5.           int exp = base;
  6.           while (--power > 0) exp *= base;
  7.           return exp;
  8.      }
  9.      else return 1;
  10. }
  11.  
  12. //Alpha to Binary
  13. void ATOB(char a,int* dest)
  14. {     
  15.      int power = 0, rem = a;
  16.      for (int i = 0;i < 8;i++)
  17.      {
  18.           power = POW(2,7-i);
  19.           *dest = rem / power; dest++;
  20.           rem %= power;
  21.      }
  22. }
  23. void ATOB(char a,char* dest)
  24. {     
  25.      int power = 0, rem = a;
  26.      for (int i = 0;i < 8;i++)
  27.      {
  28.           power = POW(2,7-i);
  29.           *dest = rem / power; dest++;
  30.           rem %= power;
  31.      }
  32. }
  33.  
  34. void ATOB(char a,bool* dest)
  35. {     
  36.      int power = 0, rem = a;
  37.      for (int i = 0;i < 8;i++)
  38.      {
  39.           power = POW(2,7-i);
  40.           *dest = rem / power; dest++;
  41.           rem %= power;
  42.      }
  43. }
  44.  
  45. //Alpha to Hexidecimal
  46. void ATOH(char a,char* dest)
  47. {
  48.      int power = 0, rem = a, bin[3];
  49.      for (int i = 0;i < 3;i++)
  50.      {
  51.           power = POW(16,2-i);
  52.           bin[i] = rem / power;
  53.           rem %= power;
  54.      }
  55.      if (bin[0] > 0)
  56.      {
  57.           *dest = 48 + bin[0]; dest++;
  58.           if (bin[1] > 9) *dest = (65 + ((bin[1] * 10) + bin[2]) % 10);
  59.           else *dest = 48 + bin[2];
  60.      }
  61.      else if (bin[0] == 0)
  62.      {
  63.           *dest = 48 + bin[1]; dest++;
  64.           if (bin[2] > 9) *dest = 65 + (bin[2] % 10);
  65.           else  *dest = 48 + bin[2];
  66.      }
  67. }
  68.  
  69. //Alpha to Octal
  70. void ATOO(char a,char* dest)
  71. {
  72.      int power = 0, rem = a;
  73.      for (int i = 0;i < 4;i++)
  74.      {
  75.           power = POW(8,3-i);
  76.           *dest = 48 + (rem / power); dest++;
  77.           rem %= power;
  78.      }
  79. }
  80. void ATOO(char a,int* dest)
  81. {
  82.      int power = 0, rem = a;
  83.      for (int i = 0;i < 4;i++)
  84.      {
  85.           power = POW(8,3-i);
  86.           *dest = rem / power; dest++;
  87.           rem %= power;
  88.      }
  89. }
  90.  
  91. //Binary to Alpha
  92. char BTOA(int bin[8])
  93. {
  94.      int a = 0;
  95.      for (int i = 0;i < 8;i++) if (bin[i] == 1)
  96.           a += POW(2,i);
  97.  
  98.      return a;
  99. }
  100. char BTOA(bool bin[8])
  101. {
  102.      int a = 0;
  103.      for (int i = 0;i < 8;i++) if (bin[i] == 1)
  104.           a += POW(2,i);
  105.  
  106.      return a;
  107. }
  108.  
  109. char BTOA(char bin[8])
  110. {
  111.      int a = 0;;
  112.      for (int i = 0;i < 8;i++) if (bin[i] == '1')
  113.           a += POW(2,i);
  114.  
  115.      return a;
  116. }
  117.  
  118. //Octal to Alpha
  119. char OTOA(int oct[4])
  120. {
  121.      int a = 0;
  122.      for (int i = 0;i < 4;i++) if (oct[i] > 0)
  123.           a += (oct[i] * POW(8,2-i));
  124.  
  125.      return a;
  126. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month