问题
Is there an easy way to add an offset to the hex dump generated by xxd ?
i.e instead of
0000: <data>
0004: <data>
0008: <data>
I should get
Offset+0000: <data>
Offset+0004: <data>
Offset+0008: <data>
回答1:
This is what I am doing now..It works perfectly but its kind of lame approach for just adding an offset :)
xxd file.bin | xxd -r -s 0x2e00000 | xxd -s 0x2e00000 > file.hex
回答2:
xxd
now appears to come with offset support, using -o [offset]
for example: xxd -o 0x07d20000 file.bin
My version of xxd
on Gentoo Linux has it, but I dug deeper to help folks on other distros:
xxd V1.10 27oct98 by Juergen Weigert
-- Do not use the xxd version -- I have found this source code without the offset support!! So I tracked down where my binary comes from:
app-editors/vim-core-7.4.769
-- So apparently, as long as you have a modern VIM installed, you can reap the benefits of the added offset support; at least on Gentoo, but I'm steering you in the right direction.
If you find that your distro still ships an older xxd, considering manually compiling a newer VIM that you confirm has offset support.
回答3:
Reading your comment below:
I want the first byte of binary file to be present at the offset. i.e Just add an offset without seeking.
makes me believe the only way to do this is parsing the output and modifying it in order to add the desired offset.
I didn't found anything in the docs that would allow this to be done easily, sorry. :(
回答4:
If you can live with AWK here's a proof of concept:
$ xxd random.bin | gawk --non-decimal-data ' # <-- treat 0x123 like numbers
> {
> offset = 0x1000 # <-- your offset, may be hex of dec
>
> colon = index($0, ":") - 1
> x = "0x" substr($0, 1, colon) # <-- add 0x prefix to original offset ...
> sub(/^[^:]*/, "") # <-- ... and remove it from line
>
> new = x + offset # <-- possible thanks to --non-decimal-data
> printf("%0"colon"x", new) # <-- print updated offset ...
> print # <-- ... and the rest of line
> }'
0001000: ac48 df8c 2dbe a80c cd03 06c9 7c9d fe06 .H..-.......|...
0001010: bd9b 02a1 cf00 a5ae ba0c 8942 0c9e 580d ...........B..X.
0001020: 6f4b 25a6 6c72 1010 8d5e ffe0 17b5 8f39 oK%.lr...^.....9
0001030: 34a3 6aef b5c9 5be0 ef44 aa41 ae98 44b1 4.j...[..D.A..D.
^^^^
updated offsets (+0x1000)
I bet it would be shorter in Perl or Python, but AWK just feels more "script-ish" :-)
来源:https://stackoverflow.com/questions/10233526/how-to-insert-an-offset-to-hexdump-with-xxd