Hallo Ich habe versucht, meinen Code zu erhalten, um Ergebnisse in eine Textdatei ohne Glück auszugeben. Ich würde es sehr schätzen, wenn jemand es für mich anschauen könnte. Ich versuchte fstream
und cout
ohne Glück ... schließlich habe ich nur Batch file.exe > Out.txt
verwendet, um das Ergebnis zu setzen. Gibt es das überhaupt im cpp-Code, ohne mir die Haare auszuziehen?Hex-Array-cout zu Textdateifehler
Dies ist mein aktueller Code sollte es ausgeben ... was mache ich falsch?
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
int hex_to_int(char c)
{
if (c >= 97)
c = c - 32;
int first = c/16 - 3;
int second = c % 16;
int result = first * 10 + second;
if (result > 9) result--;
return result;
}
int hex_to_ascii(char c, char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
int main(){
std::string line,text;
std::ifstream in("Input.txt");
while(std::getline(in, line))
{
text += line ;
}
const char* st = text.c_str();
int length = strlen(st);
int i;
char buf = 0;
for(i = 0; i < length; i++){
if(i % 2 != 0){
std::ofstream out("Output.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to Output.txt CrickeyMoses!
printf("%c", hex_to_ascii(buf, st[i]));
std::cout << std::flush;
std::cout.rdbuf(coutbuf);
}else{
buf = st[i];
}
}
}
Eingabe.txt:
2b524553503a4754494e462c3046303130362c3836323139333032303637373338312c2c34312c38393135343530303030303030343333353631322c33312c302c312c302c2c342e312c302c302c2c2c32303137313031323231353932332c2c2c2c30302c30302c2b303030302c302c32303137313031323232303032322c3534344324
0A
2b524553503a4754494e462c3046303130362c3836323139333032303637373338312c2c34312c38393135343530303030303030343333353631322c33312c302c312c302c2c342e312c302c302c2c2c32303137313031323231353932332c2c2c2c30302c30302c2b303030302c302c32303137313031323232303032322c3534344324
Output.txt:
+RESP:GTINF,0F0106,862193020677381,,41,89154500000004335612,31,0,1,0,,4.1,0,0,,,20171012215923,,,,00,00,+0000,0,20171012220022,544C$
+RESP:GTINF,0F0106,862193020677381,,41,89154500000004335612,31,0,1,0,,4.1,0,0,,,20171012215923,,,,00,00,+0000,0,20171012220022,544C$
Hex in ASCII-C++ Decoder
Was ist falsch mit der Verwendung des ['std :: hex'] (http://en.cppreference.com/w/cpp/io/manip/hex) I/O-Manipulators zum Scannen von Schriften? – user0042