hexdump format strings

Tue 13 December 2005

In tips.

Ian Wienand

More from the "things you'd learn if you read the manual" department, although the manual as currently written is a little esoteric on this feature of hexdump.

$ hexdump -e ' [iterations]/[byte_count] "[format string]" '

As you might guess, this means apply format string to groups of byte_count bytes, iterations times. Format string is likeprintf.

You can of course chain multiple formats together, or put them in a file. So say you needed to clag some binary data into a C array, a-la firmware for loading into a driver. You could use

$ hexdump -v -e '6/4 "0x%08x, "' -e '"\n"' ./dump

to get something that fits in 80 columns and is almost ready to go.

Manual on How to Use the Hexdump Unix Utility

Every time I go to use hexdump I find myself stumbling over it's bizarre argument format and ineffectual manual. Because I look at binary files often I decided to work up this page more for myself than anyone else.

Here is a sample usage of hexdump:

cat file | hexdump -v -e '"\\\x" 1/1 "%02x" " "'

And here is some sample output from it:

\x97\x6d\xea\xd1\x21\x02\x12\x80\x01\x9d...

Argument Description

The -v argument is needed to not print duplicate chars as a *. The -e argument has 5 parts separated by whitespace. I'm not sure how multiple format entries interact.

  1. An optional starting string (in quotes) which is printed before each conversion. In the first example below we are printing x before each hex character ("x").
  2. An interation count which defaults to 1 if not supplied but has to be supplied if you want a byte count. This tells how many times to do the conversion before we print the end string. So if you were decoding 4 things, each of 1 byte, you'd say 4/1.
  3. A byte count which is separated from the interation count by a /. I don't think there is a way to specify a byte count without an interation count. This specifies how many bytes are in each item that is being decoded.
  4. A sprintf format. The manual is somewhat useful here. Some standard ones apply and there are _ extensions. In the first example below this is "%02X" which prints each byte as a 2 character, 0 prefixed capital hex number.
  5. An optional ending string (in quotes) which is printed after the conversion. A space in the first example below (" ").

Format Examples

Print an "x" to start, take 1 item / 1 byte, print it as a 2 digit 0 prefixed hex value ("%02X"), and end with a space.

echo "hello there" | hexdump -v -e '"x" 1/1 "%02X" " "'

Outputs something like:

x68 x65 x6C x6C x6F x20 x74 x68 x65 x72 x65 x0A

Print an "[" to start, take 2 items / each 1 byte, print them as a 3 digit 0 prefixed octal value followed by a space ("%03o "), and end with "]\n".

echo "hello there" | hexdump -v -e '"[" 2/1 "%03o " "]\n"'

Outputs something like:

[150 145]
[154 154]
[157 040]
[164 150]
[145 162]
[145 012]

NOTE: the trailing space in "%03o " format will not be printed at the end of each interation.

hexdump related.的更多相关文章

  1. MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems

          早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...

  2. Linux命令学习总结:hexdump

    命令简介: hexdump是Linux下的一个二进制文件查看工具,它可以将二进制文件转换为ASCII.八进制.十进制.十六进制格式进行查看. 指令所在路径:/usr/bin/hexdump 命令语法: ...

  3. 用hexdump获取event的输出信息

    当我们在调试输入设备时,如:键盘,触摸屏 会使用到hexdump工具.其内容如下: 1. 键盘: # cat /dev/input/event0 | hexdump 0000000 f6a6 4e15 ...

  4. 使用 hexdump dump 文件内容

    名词解释 [dump] dump 是指把文件的内容,每个字节用2位十六进制数来表示的方式. 缘由 最近看矢泽久雄的<How Program Works>,了解到 dump “exe文件”( ...

  5. How to remove a batch of VMs and related Disks

    Foreword Need to remove a batch of VMs, which named with same prefix or belong to same Cloud Service ...

  6. hexdump—Linux系统的二进制文件查看工具

    hexdump 无参: 相当于 hexdump -x 0000000 457f 464c 0102 0001 0000 0000 0000 0000 0000010 0002 003e 0001 00 ...

  7. SharePoint Server 2010 & WorkFlow related Limits

    Today, I have come across different workflow related limits for SharePoint Server 2010. Limit Maximu ...

  8. HexDump.java解析

    从包名我们可以看出该类并没有对应用开发者开放,也就是说在google开放的Android API文档中并没有该类的相关介绍:好在Android系统源码是开源的,因此我在解决framework中问题的时 ...

  9. PeopleSoft Related Language Records

    As we all know, PeopleSoft is capable of maintaining application data in multiple languages within t ...

随机推荐

  1. BZOJ3771 Triple 【NTT + 容斥】

    题目链接 BZOJ3771 题解 做水题放松一下 先构造\(A_i\)为\(x\)指数的生成函数\(A(x)\) 再构造\(2A_i\)为指数的生成函数\(B(x)\) 再构造\(3A_i\)为指数的 ...

  2. Spring切面之一

    为什么要使用AOP,在编写程序的时候,除了不必关心依赖的组件如何实现,在实际开发过程中,还需要将程序中涉及的公共问题集中解决.AOP是Aspect-Oriented Programming的简称,意思 ...

  3. Linux(一)——认识Linux

    一.Linux介绍 (安装的是Centos6.7) 1.Linux 系统是一套免费使用和自由传播的类 Unix 操作系统(主要用在服务器上),是一个基于 POSIX 和 UNIX 的多用户.多任务.支 ...

  4. bzoj3173: [Tjoi2013]最长上升子序列(fhqtreap)

    这题用fhqtreap可以在线. fhqtreap上维护以i结尾的最长上升子序列,数字按从小到大加入, 因为前面的数与新加入的数无关, 后面的数比新加入的数小, 所以新加入的数对原序列其他数的值没有影 ...

  5. C++ STL 一般总结(转载)

    注:原博地址:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/22/2603525.html 以下内容来源网上 经过整合而成 一.一般介绍 STL ...

  6. 「PLC」PLC的硬件与工作原理

  7. golang channel状态表

    如果我们查看该表,可以察觉到在操作中可能产生问题的地方.这里有三个可能导致阻塞的操作,以及三 个可能导致程序恐慌的操作. 乍看之下,通道的使用上限制很多,但在检查了这个限制产生的动机并熟悉 了通道的使 ...

  8. Jenkins使用教程之管理节点

    通常的情况下在我们的一个项目当中,项目会有多个分支系统,而我们不可能为每个分支系统都配置一个jenkins服务,这样既浪费资源,也增加构建部署的难度,为了解决这个问题jenkins给使用者提供了非常强 ...

  9. package.json文档

    之前在博客中写过一篇关于 " node.js的安装配置 " 的文章,里面有提到利用 gulp watch 来监听文档的变化.其中需要 package.json 文件才能实现效果,所 ...

  10. uva 557 Burger

    https://vjudge.net/problem/UVA-557 题意: n个人,n/2个牛肉煲,n/2个鸡肉堡 每次抛硬币,根据正反决定每个人吃什么汉堡 如果某一个汉堡被选完了,就不抛了 问最后 ...