html tool

显示标签为“xxd”的博文。显示所有博文
显示标签为“xxd”的博文。显示所有博文

2023年11月8日星期三

转:vim打开二进制文件

 参考:https://blog.csdn.net/haohaibo031113/article/details/73604068

1) 以二进制方式编辑文件: vim -b ${datafile}

2)用xxd把此文件转换为十六进制: :%!xxd



编辑文件右侧文本(直接编辑左侧十六进制不导致字符改变,反之亦然)

3)最后用: :%!xxd -r 将其转换回来,这样只有十六进制部分才被采用。否则右侧可显示文本部分的修改会忽略不计的。


PS: xxd是linux命令,vim调用!来使用此外部命令,功能就是进行十六进制的dump或反之。


2019年12月12日星期四

xxd转储二进制文件和再次使用


参考: http://www.voidcn.com/article/p-aerutyvh-buz.html

问题: 

1. 将文件使用二进制字节流方式存储到ES中,
2. 从ES中获得对应的二进制字节流转储为文件使用

方案:

1. xxd -p ${filename} 就可以直接存了
2. cat ${二进制字节流}|xxd -r -p >${使用文件}
其中${二进制字节流} 从es直接get/search就可以

之前卡在each -b 的内容存储无法做二进制原文保存,这里看来xxd -r -p就是用来解决这个问题的:)

2019年1月21日星期一

xxd -seek


https://blog.csdn.net/sahusoft/article/details/6871635
https://stackoverrun.com/cn/q/11503594


# read 1 byte at offset 40C 
b_hex=$(xxd -seek $((16#40C)) -l 1 -ps A.bin -)  【popexizhi: 这里的偏移16设置40C,为什么自己测试不成功呢?】
# delete 3 least significant bits 
b_dec=$(($((16#$b_hex)) & $((2#11111000)))) 
cp A.bin B.bin 
# write 1 byte back at offset 40C 
printf "00040c: %02x" $b_dec | xxd -r - B.bin 
# xxd --help
Usage:
xxd [options] [infile [outfile]]
or
xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
-a toggle autoskip: A single '*' replaces nul-lines. Default off.
-b binary digit dump (incompatible with -p,-i,-r). Default hex.
-c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
-E show characters in EBCDIC. Default ASCII.
-g number of octets per group in normal output. Default 2. 每个goup的字节数,默认为2,可设置。
-h print this summary.
-i output in C include file style. :输出为c包含文件的风格,数组方式存在。
-l len stop after <len> octets. :转换到len个字节后停止转换。
-ps output in postscript plain hexdump style.
-r reverse operation: convert (or patch) hexdump into binary.
-r -s off revert with <off> added to file positions found in hexdump.
-s [+][-]seek start at <seek> bytes abs. (or +: rel.) infile offset.
-u use upper case hex letters. : 字节大写方式
-v show version: "xxd V1.10 27oct98 by Juergen Weigert".

xxd-linux 直接修改二进制方式


https://zhuanlan.zhihu.com/p/24883064

读取文件,二进制显示
$ xxd file1
00000000: 736f 6d65 2063 6f6e 7465 6e74 206f 6620  some content of
00000010: 6669 6c65 310a                           file1.
xxd 可以做反向 dump ,这就很“魔法”了(手册里作者原话)。 怎么 revert dump 呢?直接上代码
$ xxd file1 file1.dump
$ cat file1.dump 
00000000: 736f 6d65 2063 6f6e 7465 6e74 206f 6620  some content of 
00000010: 6669 6c65 310a                           file1.
$ vim file1.dump 
# magic editing...
$ cat file1.dump
00000000: 736f 6d65 4d41 4749 4321 6e74 206f 6620  some content of 
00000010: 6669 6c65 310a                           file1.
$ xxd -r file1.dump 
someMAGIC!nt of file1
很简单吧?直接加 -r 参数就能做反向 dump 。所以一般步骤是先 xxd TARGET DUMP_FILE 导出,然后修改 DUMP_FILE ,最后 xxd -r DUMP_FILE NEW_FILE (没有NEW_FILE 的时候默认输出到标准输出)。[popexizhi: 这个确实好魔幻啊,赞一个]

PS:
简单说下 vim 集成:
  • 先用二进制方式打开文件 vim FILE -b
  • 在正常模式下输入魔法 :%!xxd
  • 修改
  • 退出到正常模式,继续魔法 :%!xxd -r
用 -b 选项(即二进制模式)打开文件很重要啊!!!