Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <functional>
- #include <typeinfo>
- #include <cstdio>
- using namespace std;
- void dec2bin(int decimal, char *binary);
- void dec2bin(int decimal, char *binary)
- {
- int k = 0, n = 0;
- int neg_flag = 0;
- int remain;
- int old_decimal;
- char temp[80];
- if (decimal < 0)
- {
- decimal = -decimal;
- neg_flag = 1;
- }
- do
- {
- old_decimal = decimal;
- remain = decimal % 2;
- decimal = decimal / 2;
- temp[k++] = remain + '0';
- } while (decimal > 0);
- if (neg_flag)
- temp[k++] = '-';
- else
- temp[k++] = ' ';
- while (k >= 0)
- binary[n++] = temp[--k];
- binary[n-1] = 0;
- }
- void split(const string& s, char c,
- vector<string>& v) {
- string::size_type i = 0;
- string::size_type j = s.find(c);
- while (j != string::npos) {
- v.push_back(s.substr(i, j-i));
- i = ++j;
- j = s.find(c, j);
- if (j == string::npos)
- v.push_back(s.substr(i, s.length( )));
- }
- }
- int bin2dec(char *bin)
- {
- int b, k, m, n;
- int len, sum = 0;
- len = strlen(bin) - 1;
- for(k = 0; k <= len; k++)
- {
- n = (bin[k] - '0'); // char to numeric value
- if ((n > 1) || (n < 0))
- {
- puts("\n\n ERROR! BINARY has only 1 and 0!\n");
- return (0);
- }
- for(b = 1, m = len; m > k; m--)
- {
- // 1 2 4 8 16 32 64 ... place-values, reversed here
- b *= 2;
- }
- // sum it up
- sum = sum + n * b;
- //printf("%d*%d + ",n,b); // uncomment to show the way this works
- }
- return(sum);
- }
- int main(int argc, char *argv[]){
- //cout << "Pokemon World - MapConverter - Version: 0.4" << endl;
- if (argc < 3){
- cout << "Syntax: ./mapconv <input> <input_format> <output_format>";
- exit(1);
- }
- // vars definition
- char *inputFile;
- string output_format = "";
- string format_type;
- format_type = argv[2];
- output_format = argv[3];
- if ((format_type != "bin") && (format_type != "hex")) {
- cout << "Input Format must be either 'bin' or 'hex'" << endl;
- cout << "Not: " << format_type;
- cout << endl;
- exit(1);
- }
- if ((output_format != "bin") && (output_format != "hex")) {
- cout << "Output Format must be either 'bin' or 'hex'" << endl;
- cout << "Not: " << output_format;
- cout << endl;
- exit(1);
- }
- inputFile = argv[1];
- FILE *inp;
- inp = fopen(inputFile, "r");
- vector<string> v;
- vector<string> b;
- string m;
- string sl;
- char buffer [100];
- char hexbuffer [100];
- unsigned int ITEM;
- int wx;
- char binary [80];
- if (inp == NULL) perror ("Error opening file");
- else {
- while (!feof(inp)) {
- fgets(buffer, 100, inp);
- m += buffer;
- }
- fclose(inp);
- }
- // split m
- string m_backup = m;
- split(m, '\n', v);
- int LINE_COUNTER = 0;
- int BREAK_COUNTER = 0;
- int HX = 0;
- char outBuffer[256];
- for (int row = 0; row < v.size(); ++row) {
- split(v[row], ',', b);
- for (int itm = 0; itm < b.size(); ++itm) {
- LINE_COUNTER++;
- if (format_type == "hex"){ // from hex2<format>
- sscanf(b[itm].c_str(), "0x%X", &ITEM);
- }
- else {
- // first replace (0b to 0x (hex-m))
- b[itm].replace(0,1, "0x");
- sscanf(b[itm].c_str(), "0x%X", &ITEM);
- }
- wx = ITEM;
- if (output_format == "bin"){
- dec2bin(wx, binary);
- sl = sl + binary;
- }
- else {
- sprintf(outBuffer, "%x", wx);
- sl = sl + outBuffer;
- }
- if (LINE_COUNTER == 20) { LINE_COUNTER = 0; BREAK_COUNTER++; sl = sl + "\n"; }
- else { sl = sl + ","; }
- if (BREAK_COUNTER == 20) { break; }
- }
- }
- // write sl to new_mapfile
- cout << sl;
- // AN DIESER STELLE VERSAGT DAS MENSCHLICHE GEHIRN. DENN ICH BIN UNFÄHIG.
- }
Add Comment
Please, Sign In to add comment