Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;程序,将常规文件转换为十六进制和二进制_C++ - Fatal编程技术网

C++ C++;程序,将常规文件转换为十六进制和二进制

C++ C++;程序,将常规文件转换为十六进制和二进制,c++,C++,我的hextump接受一个文件并输出该文件的ASCII十六进制值,以及该文件的二进制值。在这个例子中,当我在一个包含“Hello World!”的文本文件上运行我的程序时,我得到 4865 6C6C 6F20等。因此我知道它正在将直接ascii字符转换为正确的十六进制值。但是,我的课程大纲中的一个例子表明,其他字节应该在文本之前打印 这使我相信我没有正确读取整个文件。我想我可能需要使用fread()或fopen()?请帮帮我。我是C++初学者。这是我在txt文件上运行的代码: #include

我的hextump接受一个文件并输出该文件的ASCII十六进制值,以及该文件的二进制值。在这个例子中,当我在一个包含“Hello World!”的文本文件上运行我的程序时,我得到 4865 6C6C 6F20等。因此我知道它正在将直接ascii字符转换为正确的十六进制值。但是,我的课程大纲中的一个例子表明,其他字节应该在文本之前打印

这使我相信我没有正确读取整个文件。我想我可能需要使用fread()或fopen()?请帮帮我。我是C++初学者。这是我在txt文件上运行的代码:

#include <iostream>
#include <iostream>
#include <fstream> 
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <bitset>
using namespace std;

string stringToHexOrByte(char input, bool toByte)
{
    string output;

    if (toByte) {
        bitset<8> temp(input);
        output = temp.to_string();
    }
    else {
        static const char hex_digits[] = "0123456789ABCDEF";
        output.reserve(2);
        output.push_back(hex_digits[input >> 4]);
        output.push_back(hex_digits[input & 15]);
    }

    return output;
}

int main(int argc, char* argv[]) {
    // Create a text string, which is used to output the text file
    string initialInput;
    // char variable in order to read char by char from file
    char myChar;
    //bool value to know if we have the byte parameter called
    bool byteOption;
    // string which will retain the converted char to hex or byte
    string convChar;
    string fileName;
    //Check if we have -b byte Option
    if (argc > 2) {
        byteOption = true;
        fileName = argv[2];
    }
    else {
        byteOption = false;
        fileName = argv[1];
    }
    // Read from the text file
    fstream myFile(fileName, fstream::in);
    // we will use this group variable to separate the chars printed as in the examples
    int group = 1;
    while (myFile >> std::noskipws >> myChar) {

        // if the bynary option is selected we will print all the chars separetely
        if (byteOption) {
            group = 0;
        }

        convChar = stringToHexOrByte(myChar, byteOption);
        cout << convChar;

        // here we use the group to get the output expected
        if (group) {
            group = 0;
        }
        else {
            cout << " ";
            group = 1;
        }

        //clear non-printable chars
        if ((int)myChar < 32) {
            myChar = '.';
        }

        // Append to input string
        initialInput.append(1, myChar);
    }
    // Close the file
    myFile.close();

    //print input
    cout << "  " << initialInput;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串stringToHexOrByte(字符输入,布尔toByte)
{
字符串输出;
如果(托比特){
位集温度(输入);
输出=到_字符串()的温度;
}
否则{
静态常量字符十六进制数字[]=“0123456789ABCDEF”;
产出.储备(2);
输出。推回(十六进制数字[输入>>4]);
输出。推回(十六进制数字[input&15]);
}
返回输出;
}
int main(int argc,char*argv[]){
//创建用于输出文本文件的文本字符串
字符串初始输入;
//char变量,以便从文件中逐字符读取字符
char-myChar;
//bool值,以了解是否调用了byte参数
布尔字节选项;
//字符串,该字符串将保留转换为十六进制或字节的字符
字符串convChar;
字符串文件名;
//检查是否有-b字节选项
如果(argc>2){
字节选项=真;
fileName=argv[2];
}
否则{
字节选项=false;
fileName=argv[1];
}
//从文本文件中读取
fstream myFile(文件名,fstream::in);
//我们将使用此组变量来分隔示例中打印的字符
int组=1;
while(myFile>>std::noskipws>>myChar){
//如果选择bynary选项,我们将单独打印所有字符
if(字节选项){
组=0;
}
convChar=stringToHexOrByte(myChar,字节选项);

难道不需要强制转换为
int
来比较其ASCII值,只要
if(myChar<32)
很好,
char
是一种数字类型。您可能希望使用
unsigned char
强制使用unsigned char,这样就不会在其中得到无意义的负值。您还必须处理与UTF-8相关的字符编码问题。您的示例还有哪些字节?可能他们正在处理的文件有UTF(字节
FE-FF
FF-FE
)。除非您将文件另存为UTF-16,否则大多数文本编辑器不会发出这些字节中的一个(即使这样,有些编辑器也不会写入,除非您明确告诉它们),因此它可能与您如何读取文件关系不大,而与您如何写入文件关系更大。@MilesBudnek示例文件输出0504 06,然后与我的输出相同。听起来不像是BOM表。一般来说,文本文件在第一个字符之前不应该有任何其他字节(BOM除外)。这可能是一个带有额外不可打印字符的输出示例,也可能是您没有提到的其他要求。