Guest User

Untitled

a guest
Oct 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <functional>
  7. #include <typeinfo>
  8. #include <cstdio>
  9.  
  10. using namespace std;
  11.  
  12. void dec2bin(int decimal, char *binary);
  13.  
  14. void dec2bin(int decimal, char *binary)
  15. {
  16. int k = 0, n = 0;
  17. int neg_flag = 0;
  18. int remain;
  19. int old_decimal;
  20. char temp[80];
  21.  
  22. if (decimal < 0)
  23. {
  24. decimal = -decimal;
  25. neg_flag = 1;
  26. }
  27. do
  28. {
  29. old_decimal = decimal;
  30. remain = decimal % 2;
  31. decimal = decimal / 2;
  32. temp[k++] = remain + '0';
  33. } while (decimal > 0);
  34.  
  35. if (neg_flag)
  36. temp[k++] = '-';
  37. else
  38. temp[k++] = ' ';
  39. while (k >= 0)
  40. binary[n++] = temp[--k];
  41.  
  42. binary[n-1] = 0;
  43. }
  44. void split(const string& s, char c,
  45. vector<string>& v) {
  46. string::size_type i = 0;
  47. string::size_type j = s.find(c);
  48. while (j != string::npos) {
  49. v.push_back(s.substr(i, j-i));
  50. i = ++j;
  51. j = s.find(c, j);
  52. if (j == string::npos)
  53. v.push_back(s.substr(i, s.length( )));
  54. }
  55. }
  56.  
  57. int bin2dec(char *bin)
  58. {
  59. int b, k, m, n;
  60. int len, sum = 0;
  61.  
  62. len = strlen(bin) - 1;
  63. for(k = 0; k <= len; k++)
  64. {
  65. n = (bin[k] - '0'); // char to numeric value
  66. if ((n > 1) || (n < 0))
  67. {
  68. puts("\n\n ERROR! BINARY has only 1 and 0!\n");
  69. return (0);
  70. }
  71. for(b = 1, m = len; m > k; m--)
  72. {
  73. // 1 2 4 8 16 32 64 ... place-values, reversed here
  74. b *= 2;
  75. }
  76. // sum it up
  77. sum = sum + n * b;
  78. //printf("%d*%d + ",n,b); // uncomment to show the way this works
  79. }
  80. return(sum);
  81. }
  82.  
  83. int main(int argc, char *argv[]){
  84. //cout << "Pokemon World - MapConverter - Version: 0.4" << endl;
  85. if (argc < 3){
  86. cout << "Syntax: ./mapconv <input> <input_format> <output_format>";
  87. exit(1);
  88. }
  89. // vars definition
  90. char *inputFile;
  91. string output_format = "";
  92. string format_type;
  93. format_type = argv[2];
  94. output_format = argv[3];
  95. if ((format_type != "bin") && (format_type != "hex")) {
  96. cout << "Input Format must be either 'bin' or 'hex'" << endl;
  97. cout << "Not: " << format_type;
  98. cout << endl;
  99. exit(1);
  100. }
  101. if ((output_format != "bin") && (output_format != "hex")) {
  102. cout << "Output Format must be either 'bin' or 'hex'" << endl;
  103. cout << "Not: " << output_format;
  104. cout << endl;
  105. exit(1);
  106. }
  107. inputFile = argv[1];
  108. FILE *inp;
  109. inp = fopen(inputFile, "r");
  110. vector<string> v;
  111. vector<string> b;
  112. string m;
  113. string sl;
  114. char buffer [100];
  115. char hexbuffer [100];
  116. unsigned int ITEM;
  117. int wx;
  118. char binary [80];
  119. if (inp == NULL) perror ("Error opening file");
  120. else {
  121. while (!feof(inp)) {
  122. fgets(buffer, 100, inp);
  123. m += buffer;
  124. }
  125. fclose(inp);
  126. }
  127. // split m
  128. string m_backup = m;
  129. split(m, '\n', v);
  130. int LINE_COUNTER = 0;
  131. int BREAK_COUNTER = 0;
  132. int HX = 0;
  133. char outBuffer[256];
  134. for (int row = 0; row < v.size(); ++row) {
  135. split(v[row], ',', b);
  136. for (int itm = 0; itm < b.size(); ++itm) {
  137. LINE_COUNTER++;
  138. if (format_type == "hex"){ // from hex2<format>
  139. sscanf(b[itm].c_str(), "0x%X", &ITEM);
  140. }
  141. else {
  142. // first replace (0b to 0x (hex-m))
  143. b[itm].replace(0,1, "0x");
  144. sscanf(b[itm].c_str(), "0x%X", &ITEM);
  145. }
  146. wx = ITEM;
  147. if (output_format == "bin"){
  148. dec2bin(wx, binary);
  149. sl = sl + binary;
  150. }
  151. else {
  152. sprintf(outBuffer, "%x", wx);
  153. sl = sl + outBuffer;
  154. }
  155.  
  156. if (LINE_COUNTER == 20) { LINE_COUNTER = 0; BREAK_COUNTER++; sl = sl + "\n"; }
  157. else { sl = sl + ","; }
  158. if (BREAK_COUNTER == 20) { break; }
  159. }
  160. }
  161. // write sl to new_mapfile
  162. cout << sl;
  163. // AN DIESER STELLE VERSAGT DAS MENSCHLICHE GEHIRN. DENN ICH BIN UNFÄHIG.
  164.  
  165. }
Add Comment
Please, Sign In to add comment