Linux命令:hexdump
hexdump是Linux下的一个二进制文件查看工具,它可以将二进制文件转换为ASCII、八进制、十进制、十六进制格式进行查看。
指令所在路径:/usr/bin/hexdump
命令语法:
hexdump: [-bcCdovx] [-e fmt] [-f fmt_file] [-n length] [-s skip] [file ...]
命令参数:
此命令参数是Red Hat Enterprise Linux Server release 5.7下hexdump命令参数,不同版本Linux的hexdump命令参数有可能不同。
|
参数 |
长参数 |
描叙 |
|
-b |
每个字节显示为8进制。一行共16个字节,一行开始以十六进制显示偏移值 |
|
|
-c |
每个字节显示为ASCII字符 |
|
|
-C |
每个字节显示为16进制和相应的ASCII字符 |
|
|
-d |
两个字节显示为10进制 |
|
|
-e |
格式化输出 |
|
|
-f |
Specify a file that contains one or more newline separated format strings. Empty lines and lines whose first non-blank character is a hash mark (#) are ignored. |
|
|
-n |
只格式前n个长度的字符 |
|
|
-o |
两个字节显示为8进制 |
|
|
-s |
从偏移量开始输出 |
|
|
-v |
The -v option causes hexdump to display all input data. Without the -v option, any number of groups of output lines, which would be identical to the immediately preceding group of output lines |
|
|
-x |
双字节十六进制显示 |
使用示例:
1: 查看hexdmp命令的帮助信息
[root@DB-Server ~]# man hexdump
2: 以8进制显示文件里面的字符。
[root@DB-Server ~]# cat >test.txt
ABCDEF
GHIJKM
123456
[root@DB-Server ~]# hexdump -b test.txt
0000000 101 102 103 104 105 106 012 107 110 111 112 113 115 012 061 062
0000010 063 064 065 066 012
0000015
注意:一行共16个字节,一行开始以十六进制显示偏移值(如下所示,第一行字符串只显示到D,第十六个字节,后面的F12*DFDF换行显示)
[root@DB-Server ~]# cat >test.txt
ABCDEFGHIJKLMNODF12*DFDF
[2]+ Stopped cat > test.txt
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# hexdump -b test.txt
0000000 101 102 103 104 105 106 107 110 111 112 113 114 115 116 117 104
0000010 106 061 062 052 104 106 104 106 012
0000019
[root@DB-Server ~]# hexdump -c test.txt
0000000 A B C D E F G H I J K L M N O D
0000010 F 1 2 * D F D F \n
0000019
3:以ASCII字符显示文件中字符
[root@DB-Server ~]# hexdump -c test.txt
0000000 A B C D E F G H I J K L M N O D
0000010 F 1 2 * D F D F \n
0000019
hexdump 以ASCII字符显示时,可以输出换行符,这个功能可以用来检查文件是Linux的换行符格式还是Widows格式换行符。如下所示
4:以16进制和相应的ASCII字符显示文件里的字符
[root@DB-Server ~]# hexdump -C test.txt
00000000 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 44 |ABCDEFGHIJKLMNOD|
00000010 46 31 32 2a 44 46 44 46 0a |F12*DFDF.|
00000019
5:只格式文件中前n个字符
[root@DB-Server ~]# hexdump -C -n 5 test.txt
00000000 41 42 43 44 45 |ABCDE|
00000005
6:以偏移量开始格式输出。如下所示指定参数-s 5 ,前面的ABCDE字符没有了。
[root@DB-Server ~]# hexdump -C test.txt
00000000 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 44 |ABCDEFGHIJKLMNOD|
00000010 46 31 32 2a 44 46 44 46 0a |F12*DFDF.|
00000019
[root@DB-Server ~]# hexdump -C -s 5 test.txt
00000005 46 47 48 49 4a 4b 4c 4d 4e 4f 44 46 31 32 2a 44 |FGHIJKLMNODF12*D|
00000015 46 44 46 0a |FDF.|
使用举例
C语言保存二进制文件test
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp_in;
fp_in = fopen("test", "wb");
if (fp_in == NULL) {
printf("open test failed\n");
return -1;
}
else {
for (unsigned char i = 0; i < 100; i++) {
fwrite(&i ,sizeof(unsigned char), 1, fp_in);
}
fclose(fp_in);
}
return 0;
}
编译:g++ main.cpp -o main
执行:./main即可生成二进制文件test
格式化输出文件
hexdump test
格式化输出文件的前10个字节
hexdump -n 10 test
格式化输出文件的前10个字节,并以16进制显示
hexdump -n 10 -C test
格式化输出从10开始的10个字节,并以16进制显示
hexdump -n 10 -C -s 20
格式化输出文件字符
hexdump -e ‘16/1 “%02X ” ” | “’ -e ‘16/1 “%_p” “\n”’ test
hexdump -e ‘1/1 “0x%08_ax “’ -e ‘8/1 “%02X ” ” * “’ -e ‘8/1 “%_p” “\n”’ test
hexdump -e ‘1/1 “%02_ad# “’ -e ‘/1 “hex = %02X * “’ -e ‘/1 “dec = %03d | “’ -e ‘/1 “oct = %03o”’ -e ‘/1 ” _\n”’ -n 20 test
Linux命令:hexdump的更多相关文章
- Linux命令学习总结:hexdump
命令简介: hexdump是Linux下的一个二进制文件查看工具,它可以将二进制文件转换为ASCII.八进制.十进制.十六进制格式进行查看. 指令所在路径:/usr/bin/hexdump 命令语法: ...
- Linux命令之dos2unix
Linux命令之dos2unix (2011-09-22 11:24:06) 转载▼ 标签: 杂谈 Linux命令之dos2unix - 将DOS格式文本文件转换成UNIX格式 用途说明 dos2 ...
- 【改造Linux命令之rm - 删除文件或目录-】
用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...
- 十年linux命令总结
十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令 ...
- linux 命令中英文对照,收集
linux 命令中英文对照,收集 linux 命令英文全文 Is Linux CLI case-sensitive? The answer is, yes. If you try to run L ...
- linux 命令使用方法(随时更新)
1.hexdump 命令简介:hexdump是Linux下的一个二进制文件查看工具,它可以将二进制文件转换为ASCII.八进制.十进制.十六进制格式进行查看. 命令语法:hexdump: [-bcCd ...
- 《Linux命令学习手册》系列分享专栏
<Linux命令学习手册>系列分享专栏 <Linux命令学习手册>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/207 ...
- [转]Linux命令之iconv
转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...
- Linux命令——pr
参考:Linux命令——column 前言 接触这个命令的初衷是我想把一个很长的单列输出设置成多列输出,奈何column的分列输出机制太智障,直到我发现了pr 参数 pr -# 输出指定的列数. -t ...
随机推荐
- pcntl
<?php function my_pcntl_wait($childProcessCode){ $pid = pcntl_fork(); if($pid>0){ pcntl_wait($ ...
- 跨平台打开一个URL的方法
unit u_urlOpen; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System ...
- 2.Storm集群部署及单词统计案例
1.集群部署的基本流程 2.集群部署的基础环境准备 3.Storm集群部署 4.Storm集群的进程及日志熟悉 5.Storm集群的常用操作命令 6.Storm源码下载及目录熟悉 7.Storm 单词 ...
- Vue-cli中的proxyTable解决开发环境的跨域问题
https://blog.csdn.net/u012149969/article/details/80288126 https://vuejs-templates.github.io/webpack/ ...
- java语言实现对程序设计语言源文件统计字符数、单词数、行数及其他拓展功。
本次作业Github项目地址:https://github.com/YiChenglong2018/WordCount 一.项目简介 本项目的需求可以概括为:对程序设计语言源文件统计字符数.单词数.行 ...
- node.js通过回调函数获取异步函数的返回结果
html文件代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- mybatis配置和映射文件
配置: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC ...
- C# 可观察集合
static void Main() { var data = new ObservableCollection<string>(); data.CollectionChanged += ...
- BZOJ 2095 [Poi2010]Bridges (二分+最大流判断混合图的欧拉回路)
题面 nnn个点,mmm条双向边(正向与反向权值不同),求经过最大边权最小的欧拉回路的权值 分析 见 commonc大佬博客 精髓就是通过最大流调整无向边的方向使得所有点的入度等于出度 CODE #i ...
- 爬取淘宝商品数据并保存在excel中
1.re实现 import requests from requests.exceptions import RequestException import re,json import xlwt,x ...