使用Linux指令将Hex文件与二进制文件的相互转换

本文详细介绍了如何使用Linux命令`xxd`进行Hex和二进制文件的相互转换,包括基本操作、不同格式输出以及-revert选项的应用实例。
摘要由CSDN通过智能技术生成

小结

本文记录了使用Linux指令将Hex文件与二进制文件的相互转换。

问题及解决

在很多情况下需要将Hex内容的问题转换成二进制文件,另外的情况是,需要将二进制文件转换成Hex内容的文件。例如:需要进行hex字符串存在文件里进行加解密运算时,需要转换成二进制后存在文件中,再进行运算。

使用以下xdd指令:

[john@localhost ~]$ cat hex.txt   #原始文件
5fc6e719bb7a887e32f0c1fc273121a7cc036bb8d3ffa9499821743235a73391

[john@localhost ~]$ xxd -r -p hex.txt hex.bin  #转换成二进制文件

[john@localhost ~]$ cat hex.bin #输出二进制文件
_���z�~2���'1!��k����I�!t25�3�

[john@localhost ~]$ xxd hex.bin  #hexdump格式输出 
0000000: 5fc6 e719 bb7a 887e 32f0 c1fc 2731 21a7  _....z.~2...'1!.
0000010: cc03 6bb8 d3ff a949 9821 7432 35a7 3391  ..k....I.!t25.3.

[john@localhost ~]$ xxd -p hex.bin #从二进制文件转换回简单hex文件
5fc6e719bb7a887e32f0c1fc273121a7cc036bb8d3ffa9499821743235a73391

[john@localhost ~]$ 

以下是-p-r的说明:

-p | -ps | -postscript | -plain
   output in postscript continuous hexdump style. Also known as plain hexdump style.

-r | -revert
   reverse operation: convert (or patch) hexdump into binary.  If not writing to stdout,
    xxd writes into its output file without truncating it. Use the combination -r -p to
   read plain hexadecimal dumps without line number information and without a particular  col‐
   umn layout. Additional Whitespace and line-breaks are allowed anywhere.

参考

Linux, Scripting & programming: Hexdump: Hexdump a binary file and convert it back
Stackoverflow: Transform hexadecimal information to binary using a Linux command

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++程序,可以实现二进制文件和十六进制文件相互转换。该程序使用了多线程来提高转换速度。 ```c++ #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <thread> #include <mutex> using namespace std; mutex mtx; // 互斥锁,用于线程安全输出 void binaryToHex(const string& inputFileName, const string& outputFileName) { ifstream inputFile(inputFileName, ios::binary); ofstream outputFile(outputFileName); stringstream ss; ss << hex << uppercase << noshowbase << noskipws; char c; while (inputFile >> c) { ss << static_cast<int>(c); } string hexString = ss.str(); for (int i = 0; i < hexString.length(); i += 2) { string byteString = hexString.substr(i, 2); outputFile << byteString << " "; } mtx.lock(); cout << "Binary to hex conversion completed!" << endl; mtx.unlock(); } void hexToBinary(const string& inputFileName, const string& outputFileName) { ifstream inputFile(inputFileName); ofstream outputFile(outputFileName, ios::binary); stringstream ss; string hexString; while (inputFile >> hexString) { int byte = stoi(hexString, nullptr, 16); outputFile << static_cast<char>(byte); } mtx.lock(); cout << "Hex to binary conversion completed!" << endl; mtx.unlock(); } int main() { string inputFileName = "input.bin"; string hexOutputFileName = "output.hex"; string binaryOutputFileName = "output.bin"; thread t1(binaryToHex, inputFileName, hexOutputFileName); thread t2(hexToBinary, hexOutputFileName, binaryOutputFileName); t1.join(); t2.join(); return 0; } ``` 该程序使用了两个函数 `binaryToHex` 和 `hexToBinary` 来实现二进制文件和十六进制文件相互转换。这两个函数分别将输入文件转换成字符串,然后进行转换,并输出到对应的输出文件中。 在 `main` 函数中,我们创建了两个线程 `t1` 和 `t2` 分别用来执行转换函数。使用 `join` 方法等待线程执行完毕后,程序退出。 注意,在输出结果时,我们使用了一个互斥锁 `mtx` 来保证线程安全输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值