Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

3 votes
0 answers
63 views
How to display a UTF8 encoded text file as a hex dump but with the characters in the right column
This is the sample.txt Greek in UTF8: ανθρώπους. Από ανθρώπους. Από When I run the command below I get the hex output but not the corresponding characters in the right column but instead I see ......... $ xxd -l 120 -c 12 sample.txt 00000000: ceb1 cebd ceb8 cf81 cf8e cf80 ............ 0000000c: cebf...
This is the sample.txt Greek in UTF8: ανθρώπους. Από ανθρώπους. Από When I run the command below I get the hex output but not the corresponding characters in the right column but instead I see ......... $ xxd -l 120 -c 12 sample.txt 00000000: ceb1 cebd ceb8 cf81 cf8e cf80 ............ 0000000c: cebf cf85 cf82 2e0a ce91 cf80 ............ 00000018: cf8c 0ace b1ce bdce b8cf 81cf ............ 00000024: 8ecf 80ce bfcf 85cf 822e 0ace ............ 00000030: 91cf 80cf 8c0a I m looking for a way to show the actual characters as well.
user603749 (131 rep)
Mar 26, 2025, 06:29 PM • Last activity: Mar 26, 2025, 08:10 PM
13 votes
3 answers
14775 views
What is the difference between the od, hd, hexdump and xxd commands?
What is the difference between the od, hd, hexdump and xxd commands ? They are all commands for dumping files and they can all dump it in various formats such as hexadecimal, octal or binary. Why creating different programs ?
What is the difference between the od, hd, hexdump and xxd commands ? They are all commands for dumping files and they can all dump it in various formats such as hexadecimal, octal or binary. Why creating different programs ?
Nicryc (335 rep)
Jan 15, 2020, 10:47 PM • Last activity: Jan 9, 2025, 02:25 PM
29 votes
2 answers
65327 views
xxd output without line breaks
Can I tell `xxd` to not print any line breaks in its output and have my dump as one continuous line? [user@localhost] : ~ $ echo -n "this is a long line and xxd is going to take multiple lines to print it" | xxd -p 746869732069732061206c6f6e67206c696e6520616e6420787864206973 20676f696e6720746f207461...
Can I tell xxd to not print any line breaks in its output and have my dump as one continuous line? [user@localhost] : ~ $ echo -n "this is a long line and xxd is going to take multiple lines to print it" | xxd -p 746869732069732061206c6f6e67206c696e6520616e6420787864206973 20676f696e6720746f2074616b65206d756c7469706c65206c696e657320 746f207072696e74206974
Juicy (4125 rep)
Jul 26, 2015, 10:58 PM • Last activity: Jul 27, 2024, 11:25 AM
0 votes
2 answers
335 views
From hexdump to ascii (xxd -r stuck writing zero bytes)
To diagnose secure boot issues, I'm looking at the sbat sections of all bootloaders on a machine: sudo sh -c 'for f in /boot/efi/EFI/*/*.efi; do echo "$f"; objdump -j .sbat -s "$f" |xxd -r; echo; done' The idea is to compare with `strings /sys/firmware/efi/efivars/SbatLevelRT-605dab50-e046-4300-abb6...
To diagnose secure boot issues, I'm looking at the sbat sections of all bootloaders on a machine: sudo sh -c 'for f in /boot/efi/EFI/*/*.efi; do echo "$f"; objdump -j .sbat -s "$f" |xxd -r; echo; done' The idea is to compare with strings /sys/firmware/efi/efivars/SbatLevelRT-605dab50-e046-4300-abb6-3dd810dd8b23. I'm using xxd -r to go from objdump's hex output to plain ascii, but it looks like it isn't the command I want, because if the hexdump's first column contains large offsets, xxd will get stuck writing zeroes for longer than I can wait to get readable output. For example, for systemd-boot from systemd-boot-efi on ubuntu:
objdump -j .sbat -s /usr/lib/systemd/boot/efi/systemd-bootx64.efi
/usr/lib/systemd/boot/efi/systemd-bootx64.efi:     file format pei-x86-64

Contents of section .sbat:
 101319000 73626174 2c312c53 42415420 56657273  sbat,1,SBAT Vers
 101319010 696f6e2c 73626174 2c312c68 74747073  ion,sbat,1,https
 101319020 3a2f2f67 69746875 622e636f 6d2f7268  ://github.com/rh
 101319030 626f6f74 2f736869 6d2f626c 6f622f6d  boot/shim/blob/m
 101319040 61696e2f 53424154 2e6d640a 73797374  ain/SBAT.md.syst
 101319050 656d642d 626f6f74 2c312c54 68652073  emd-boot,1,The s
 101319060 79737465 6d642044 6576656c 6f706572  ystemd Developer
 101319070 732c7379 7374656d 642c3235 352c6874  s,systemd,255,ht
 101319080 7470733a 2f2f7379 7374656d 642e696f  tps://systemd.io
 101319090 2f0a7379 7374656d 642d626f 6f742e75  /.systemd-boot.u
 1013190a0 62756e74 752c312c 5562756e 74752c73  buntu,1,Ubuntu,s
 1013190b0 79737465 6d642c32 35352e34 2d317562  ystemd,255.4-1ub
 1013190c0 756e7475 382e312c 68747470 733a2f2f  untu8.1,https://
 1013190d0 62756773 2e6c6175 6e636870 61642e6e  bugs.launchpad.n
 1013190e0 65742f75 62756e74 752f0a00           et/ubuntu/..
Is there another way to unhex data while ignoring anything that comes before the first offset?
Tobu (6769 rep)
May 29, 2024, 09:10 AM • Last activity: May 29, 2024, 01:49 PM
0 votes
1 answers
3612 views
What does xxd -r -p do?
So I am trying to calculate a SHA1 digest (and base64 encode it) for an attachment I received so it matches their (base64 encoded) digest. Using ChatGPT, I was able to use the command below to succeed. But I don't understand the command `xxd -r -p` or why it's necessary. `sha1sum foo | awk '{print $...
So I am trying to calculate a SHA1 digest (and base64 encode it) for an attachment I received so it matches their (base64 encoded) digest. Using ChatGPT, I was able to use the command below to succeed. But I don't understand the command xxd -r -p or why it's necessary. sha1sum foo | awk '{print $1}' | xxd -r -p | base64 There's two things I don't understand about it: 1) Why is the option -p needed? I understand xxd -r makes a binary dump of a hexdump, but if I leave off the -p it prints nothing while the man page says it should print to stdout and that -p is just for a particular output style. 2) What is the purpose of even passing the SHA1 digest through xxd? My understanding is xxd essentially converts data between hex and binary. But when I run the above command without xxd -r -p, it gives a different, and unmatched, output. I do know what hex style/representation is (that's the format of a SHA1 digest), but perhaps I don't know what "binary" means in this context?
geckels1 (173 rep)
Nov 27, 2023, 08:10 PM • Last activity: Nov 28, 2023, 07:32 AM
0 votes
4 answers
617 views
awk command to map ASCII char to hex code not working
I'm experimenting with awk and I thought it would be a cool idea to write a small program that maps every ASCII char with its hex code. this is what ive done so far: The string is: abshdfitiggwigiwjirjgiejrigjr im keeping it simple and not including returns or tabs etc I've dumped it into a file sep...
I'm experimenting with awk and I thought it would be a cool idea to write a small program that maps every ASCII char with its hex code. this is what ive done so far: The string is: abshdfitiggwigiwjirjgiejrigjr im keeping it simple and not including returns or tabs etc I've dumped it into a file seperated by commas so that I can use awk's -F flag to recognize them as fields: a,b,s,h,d,f,i,t,i,g,g,w,i,g,i,w,j,i,r,j,g,i,e,j,r,i,g,j,r, And I've done the same for hex dump of the same file: 61,62,73,68,64,66,69,74,69,67,67,77,69,67,69,77,6a,69,72,6a,67,69,65,6a,72,69,67,6a,72,0a, I figured now since they both have the same NF, it would be easier and so I attempted the following awk command: awk -F ',' '{for(i=1;i
Mathew (243 rep)
Oct 12, 2023, 10:39 PM • Last activity: Oct 13, 2023, 08:03 PM
0 votes
1 answers
512 views
tying together the 'xxd' command with output
I have the following hex dump: 00000000: 2345 5854 5241 530a 0a31 2e20 746f 2061 #EXTRAS..1. to a 00000010: 6464 2075 7365 723a 2060 7375 646f 2075 dd user: `sudo u 00000020: 7365 7261 6464 205b 7573 6572 6e61 6d65 seradd [username 00000030: 5d60 2061 6e64 2074 6865 6e20 6073 7564 ]` and then `sud 0...
I have the following hex dump: 00000000: 2345 5854 5241 530a 0a31 2e20 746f 2061 #EXTRAS..1. to a 00000010: 6464 2075 7365 723a 2060 7375 646f 2075 dd user: `sudo u 00000020: 7365 7261 6464 205b 7573 6572 6e61 6d65 seradd [username 00000030: 5d60 2061 6e64 2074 6865 6e20 6073 7564 ] and then sud 00000040: 6f20 7061 7373 7764 205b 7573 6572 6e61 o passwd [userna 00000050: 6d65 5d60 2074 6f20 6164 6420 7061 7373 me]` to add pass 00000060: 776f 7264 0a0a 312e 2074 6865 2063 6f6d word..1. the com 00000070: 6d61 6e64 2061 626f 7665 2077 696c 6c20 mand above will 00000080: 6e6f 7420 6164 6420 6120 686f 6d65 2064 not add a home d 00000090: 6972 6563 746f 7279 2066 6f72 2074 6865 irectory for the 000000a0: 2061 6464 6564 2075 7365 720a 0a31 2e20 added user..1. 000000b0: 746f 2067 6976 6520 7468 6520 7573 6572 to give the user 000000c0: 2061 2068 6f6d 6520 6469 7265 6374 6f72 a home director 000000d0: 792c 2069 6e63 6c75 6465 2074 6865 2060 y, include the ` 000000e0: 2d6d 6020 666c 6167 0a0a 0a -m` flag... vim is telling me its 235b and simple command I wrote to count all characters is telling me there are 243 characters: xxd -ps extras.md | sed 's/\(..\)/\1\n/g' | wc -l in addition, the left memory column is telling its taking up: 'd0 + 11 bytes so 219 bytes'? If I'm understanding that right. can someone tie 219, 235 and 243 together for me? or tell me i did everything wrong that works too but let me know how to fix please
Mathew (243 rep)
Oct 11, 2023, 05:47 PM • Last activity: Oct 11, 2023, 10:07 PM
8 votes
1 answers
1101 views
Why is 0x00 being deleted when assigning a base64 decoded string to a variable
I'm using a Mac. In Bash, I'm trying to decode a base64 string and then try to print the Hex value. I'm using `base64 -d` command and then assigning it to a variable. ``` myText='YYN29+2wV2XRAHymIyhgytWuqY4atgHnIUFfXA7FPOA=' myTextBytes=$(echo -n "$myText" | base64 --decode) echo -n $myTextBytes | x...
I'm using a Mac. In Bash, I'm trying to decode a base64 string and then try to print the Hex value. I'm using base64 -d command and then assigning it to a variable.
myText='YYN29+2wV2XRAHymIyhgytWuqY4atgHnIUFfXA7FPOA='
myTextBytes=$(echo -n "$myText" | base64 --decode)
echo -n $myTextBytes | xxd -p -c 99999
The output of the above script is: > 618376f7edb05765d17ca6232860cad5aea98e1ab601e721415f5c0ec53ce0 However, if i were to run the following command directly:
echo -n "$myText" | base64 --decode | xxd -p -c 100000
I get: > 618376f7edb05765d1**00**7ca6232860cad5aea98e1ab601e721415f5c0ec53ce0 I even tried using openssl enc -base64 and I get the same result. That is, 00 is getting deleted when assigning to a variable. How do I preserve the 00 when I'm assigning it to a variable?
SilleBille (191 rep)
Aug 25, 2023, 11:11 PM • Last activity: Aug 30, 2023, 09:12 PM
1 votes
1 answers
523 views
Should the `xxd` utility be listed as a dependency in my application?
I'm distributing a desktop application and I'm unsure if `xxd` should be listed as a (build) dependency. My first though was not, since it's documented in the [Linux manpages](https://www.linux.org/docs/man1/xxd.html) and therefore should be available on all Linux machines, shoudln't it? (Correct me...
I'm distributing a desktop application and I'm unsure if xxd should be listed as a (build) dependency. My first though was not, since it's documented in the [Linux manpages](https://www.linux.org/docs/man1/xxd.html) and therefore should be available on all Linux machines, shoudln't it? (Correct me if I'm wrong) However I recently received some issues saying that the xxd was missing in their systems (Arch on Steam Deck and Debian). Doing some research found that in some distros the xxd binary is **only** distributed alongside vim as a dependency (See [this](https://discussion.fedoraproject.org/t/which-package-provides-xxd-could-not-figure-it-out/15866) and [this](https://www.reddit.com/r/archlinux/comments/5yznre/xxd_is_missing_in_neovim_package_in_arch_linux/)) , which confuses me even more, so I now depend on vim to be installed? Should I list xxd as a dependency or leave it up to the user to have the binary installed?
paoloose (113 rep)
Jul 31, 2023, 06:38 PM • Last activity: Jul 31, 2023, 06:51 PM
1 votes
1 answers
793 views
Prevent xxd from adding newline to the output
On unix if I do ``` printf "aY.S'Hk([;jYJ}8eJ5)Wsd58/x}s]Pne3V-6:t@" | xxd -p -c 256 ``` I get ``` 61592e5327486b285b3b6a594a7d38654a352957736435382f787d735d50 6e6533562d363a7440 ``` However on mac os (which is what I want on linux as well) I get ``` 61592e5327486b285b3b6a594a7d38654a352957736435382...
On unix if I do
printf "aY.S'Hk([;jYJ}8eJ5)Wsd58/x}s]Pne3V-6:t@" | xxd -p -c 256
I get
61592e5327486b285b3b6a594a7d38654a352957736435382f787d735d50 
6e6533562d363a7440
However on mac os (which is what I want on linux as well) I get
61592e5327486b285b3b6a594a7d38654a352957736435382f787d735d506e6533562d363a7440
I couldn't find any way to prevent xxd from adding a line break to hex output. I have tried sed to remove the line break but it doesn't work. I can't install tr on my unix machine. Is there any way I can create a hex output from my string that doesn't have any line break or whitespace or is there a way to configure xxd on linux to not add any whitespace? Note, I have tried pretty much all the options of xxd with no luck
java_doctor_101 (111 rep)
Feb 15, 2022, 02:51 AM • Last activity: Jun 10, 2023, 05:23 AM
4 votes
2 answers
2965 views
How to format hexdump as xxd, possible for xxd -revert?
I wish to dump the raw content of a SD card into a file for inspection. Most parts of it are zeroes. Learnt from [this SuperUser answer][1] I could use `pv` to show the progress of `od` and `hexdump`. Both ETA are 1.5 hours. # pv /dev/sdd | od -x --endian=big > sdd_file ... ... ... [> ] ... ETA 1:34...
I wish to dump the raw content of a SD card into a file for inspection. Most parts of it are zeroes. Learnt from this SuperUser answer I could use pv to show the progress of od and hexdump. Both ETA are 1.5 hours. # pv /dev/sdd | od -x --endian=big > sdd_file ... ... ... [> ] ... ETA 1:34:42 and # pv /dev/sdd | hexdump -C > sdd_file ... ... ... [> ] ... ETA 1:35:01 However xxd would need 11 hours. # pv /dev/sdd | xxd -a -u > sdd_file ... ... ... [> ] ... ETA 10:48:53 I prefer xxd mostly because of the -revert possibility. But xxd would take way too long to process a disk. How to format hexdump (or od) to produce the same file format as xxd, so that the file is possible to be -reverted by xxd?
midnite (613 rep)
Dec 18, 2021, 06:59 PM • Last activity: Jun 29, 2022, 12:33 PM
11 votes
2 answers
2743 views
How to print zeros and ones of a file
*Long story short:* how to print in a terminal the binary digits constituting a file e.g. a library *.so* or a simple text *.txt* file --- *PC hardware works with electrical signal (basically it's an ON/OFF behaviour) which is well logically translated by the binary system (digits 0s and 1s). Visual...
*Long story short:* how to print in a terminal the binary digits constituting a file e.g. a library *.so* or a simple text *.txt* file --- *PC hardware works with electrical signal (basically it's an ON/OFF behaviour) which is well logically translated by the binary system (digits 0s and 1s). Visualizing the content of a file would be an interesting **educational exercise**, as well as comparing a .txt and an executable that prints the same text.*
mattia.b89 (3398 rep)
Aug 7, 2021, 08:17 AM • Last activity: Aug 9, 2021, 07:51 PM
2 votes
1 answers
611 views
How to modify a hex string to LE-format before passing it to `xxd -r` to view its binary contents?
The hex string `0068732f6e69622f` represents the ASCII string `/bin/sh`, when it's stored in memory in LE-format. Is there any Linux utiltity that will take the hex string and reverse it bytes (`2f62696e2f736800`), such that `xxd -r -ps` will display `/bin/sh`? $ echo -n 0068732f6e69622f | xxd -r -p...
The hex string 0068732f6e69622f represents the ASCII string /bin/sh, when it's stored in memory in LE-format. Is there any Linux utiltity that will take the hex string and reverse it bytes (2f62696e2f736800), such that xxd -r -ps will display /bin/sh? $ echo -n 0068732f6e69622f | xxd -r -ps hs/nib/ I've looked into xxd -e, but it's not possible to use it with -r: -e little-endian dump (incompatible with -ps,-i,-r).
Shuzheng (4931 rep)
Jan 27, 2021, 10:45 AM • Last activity: Jan 27, 2021, 12:16 PM
2 votes
1 answers
1193 views
Make xxd break lines
For showing the content of a file with both formatted text **and** binary data inside in another post on SE I couldn't decide on wheather to show the ASCII rendered output or a the `xxd` output. As a middle course I thought about a hexdump that **does** respect newlines. (Other) example: 00000000: 6...
For showing the content of a file with both formatted text **and** binary data inside in another post on SE I couldn't decide on wheather to show the ASCII rendered output or a the xxd output. As a middle course I thought about a hexdump that **does** respect newlines. (Other) example:
00000000: 636f 6d6d 6974 2031 3933 0074 7265 6520  commit 193.tree
00000010: 3331 3439 3530 3736 3263 6561 3232 3364  314950762cea223d
00000020: 3230 6233 3931 6235 3162 3336 3463 6134  20b391b51b364ca4
00000030: 3165 3636 3234 3931 0a61 7574 686f 7220  1e662491.author
00000040: 4578 616d 706c 6520 5573 6572 203c 6578  Example User  149
00000070: 3831 3236 3633 3320 2b30 3230 300a 636f  8126633 +0200.co
00000080: 6d6d 6974 7465 7220 4578 616d 706c 6520  mmitter Example
00000090: 5573 6572 203c 6578 616d 706c 6575 7365  User  1498126633
000000c0: 2b30 3230 300a 0a69 6e69 740a            +0200..init.
would become
00000000: 636f 6d6d 6974 2031 3933 0074 7265 6520  commit 193.tree
00000010: 3331 3439 3530 3736 3263 6561 3232 3364  314950762cea223d
00000020: 3230 6233 3931 6235 3162 3336 3463 6134  20b391b51b364ca4
00000030: 3165 3636 3234 3931 0a                   1e662491.
                                
00000030:                       61 7574 686f 7220           author
00000040: 4578 616d 706c 6520 5573 6572 203c 6578  Example User  149
00000070: 3831 3236 3633 3320 2b30 3230 300a       8126633 +0200.
                                            
00000070:                                    636f                co
00000080: 6d6d 6974 7465 7220 4578 616d 706c 6520  mmitter Example
00000090: 5573 6572 203c 6578 616d 706c 6575 7365  User  1498126633
000000c0: 2b30 3230 300a                           +0200.

000000c0:                0a                              .

000000c0:                  69 6e69 740a                   init.
or
00000000: 636f 6d6d 6974 2031 3933 0074 7265 6520  commit 193.tree
00000010: 3331 3439 3530 3736 3263 6561 3232 3364  314950762cea223d
00000020: 3230 6233 3931 6235 3162 3336 3463 6134  20b391b51b364ca4
00000030: 3165 3636 3234 3931 0a                   1e662491.
00000030:                       61 7574 686f 7220           author
00000040: 4578 616d 706c 6520 5573 6572 203c 6578  Example User  149
00000070: 3831 3236 3633 3320 2b30 3230 300a       8126633 +0200.
00000070:                                    636f                co
00000080: 6d6d 6974 7465 7220 4578 616d 706c 6520  mmitter Example
00000090: 5573 6572 203c 6578 616d 706c 6575 7365  User  1498126633
000000c0: 2b30 3230 300a                           +0200.
000000c0:                0a                              .
000000c0:                  69 6e69 740a                   init.
or
00000000: 636f 6d6d 6974 2031 3933 0074 7265 6520  commit 193.tree
00000010: 3331 3439 3530 3736 3263 6561 3232 3364  314950762cea223d
00000020: 3230 6233 3931 6235 3162 3336 3463 6134  20b391b51b364ca4
00000030: 3165 3636 3234 3931 0a                   1e662491.
                                61 7574 686f 7220           author
00000040: 4578 616d 706c 6520 5573 6572 203c 6578  Example User  149
00000070: 3831 3236 3633 3320 2b30 3230 300a       8126633 +0200.
                                             636f                co
00000080: 6d6d 6974 7465 7220 4578 616d 706c 6520  mmitter Example
00000090: 5573 6572 203c 6578 616d 706c 6575 7365  User  1498126633
000000c0: 2b30 3230 300a                           +0200.
                         0a                              .
                           69 6e69 740a                   init.
or similar
stuquus (23 rep)
Jun 22, 2017, 11:38 AM • Last activity: Dec 16, 2020, 04:34 PM
0 votes
1 answers
973 views
How to `watch` output of `xxd` or `hexdump` command?
My tool is wtiting binary chars to `stdout` and I can view it in hex with # ./md /dev/ttyUSB0 | xxd 0000000: 6f03 1100 0003 0084 8400 0000 0900 0a00 o............... 0000010: 0008 0004 0000 0000 2c00 0000 0000 0000 ........,....... ... 00000b0: 8000 8000 8000 8000 8000 8000 8000 8000 ..................
My tool is wtiting binary chars to stdout and I can view it in hex with # ./md /dev/ttyUSB0 | xxd 0000000: 6f03 1100 0003 0084 8400 0000 0900 0a00 o............... 0000010: 0008 0004 0000 0000 2c00 0000 0000 0000 ........,....... ... 00000b0: 8000 8000 8000 8000 8000 8000 8000 8000 ................ 00000c0: 8047 ffff ff6f 04fd 2180 ff02 f700 f702 .G...o..!....... 00000d0: fbb6 00bf 10e1 a57f 4004 fb00 a780 7e00 ........@. unfortunately, when I am trying to watch this screen watch ./md /dev/ttyUSB0 | xxd watch ./md /dev/ttyUSB0 | hexdump It prints something like enter image description here either corrupting or misinterpretting output. What do I do wrong?
Dims (3425 rep)
Nov 21, 2020, 09:07 PM • Last activity: Nov 21, 2020, 09:13 PM
-6 votes
1 answers
455 views
Just started learning Kali Linux 3 weeks ago need help with maybe a hex dump?
For an assignment I was given this problem: >What has 8 faces and cannot see? followed by this character set: ``` 412068657861676f6e616c20707269736d21204f6b2c207468617420776173206e6f742066756e6e792e20416e79776179732c206865726520697320796f757220666c61673a0a46537b65696768745f657965735f776f756c645f6265...
For an assignment I was given this problem: >What has 8 faces and cannot see? followed by this character set:
412068657861676f6e616c20707269736d21204f6b2c207468617420776173206e6f742066756e6e792e20416e79776179732c206865726520697320796f757220666c61673a0a46537b65696768745f657965735f776f756c645f62655f615f626c696e645f7370696465725f3236313446397d
One of the hints I was given was to identify the character set first. So I was just wondering if anyone can help me identify this set.
Scalz15 (5 rep)
Oct 23, 2020, 10:36 PM • Last activity: Oct 26, 2020, 12:34 PM
1 votes
1 answers
754 views
Patch Multiple Strings in a Binary file
I have a binary file type (many instances of these files) where I would like to replace the string "DC-GX850" with "DMC-GX95". Fortunately the two strings are of equal length. The files may be as large as 22MB. Typically the string occurs twice in the file, within the first 1000 bytes. both need to...
I have a binary file type (many instances of these files) where I would like to replace the string "DC-GX850" with "DMC-GX95". Fortunately the two strings are of equal length. The files may be as large as 22MB. Typically the string occurs twice in the file, within the first 1000 bytes. both need to be replaced. My operating system is MacOS 10.14.6, running standard BASH. I have two parts of the solution working, but I am unable to combine them. For my test case, the source file is P1000047.RW2, and the modified file is P1000047D.RW2; "44434d2d47583835" is the hex representation of "DMC-GX95"; 3a4 and 10f0 are the file offsets for the start of the strings for this instance of the file; other files may have different offsets. This code correctly patches the file: echo "3a4: 44434d2d47583835" | xxd -r - P1000047D.RW2 echo "10f0: 44434d2d47583835" | xxd -r - P1000047D.RW2 This code produces the two strings needed for the patch: strings -t x P1000047.RW2 | grep "DC-GX850" | sed s/\ DC-GX850/:\ 44434d2d47583835/ Result: 3a4: 44434d2d47583835 10f0: 44434d2d47583835 However, when I try to combine the two, this code only executes the first patch strings -t x P1000047.RW2 | grep "DC-GX850" | sed s/\ DC-GX850/:\ 44434d2d47583835/ | xxd -r - P1000047D.RW2 I've tried using sed with the -n option but still unable to get it to work. I could of course write the script as a loop, but I'd like to avoid that if possible. Any suggestions?
Eric Valk (11 rep)
Oct 13, 2020, 05:20 AM • Last activity: Oct 13, 2020, 06:25 AM
0 votes
2 answers
518 views
Parse text file with addresses, byte values, and comments to binary file
I'm attempting to start with a file in the following form: ~~~ 00: 42 ; byte 0 is 0x42 / d 01: 52 ; byte 1 is 0x52 / r 02: 62 ; byte 2 is 0x62 / D 03: 72 ; byte 3 is 0x72 / R 07: 1f ; bytes 03..0e are implicitly 00, byte 0f is 0x1f ~~~ and use it to generate an 8-byte file whose values (in hex form)...
I'm attempting to start with a file in the following form: ~~~ 00: 42 ; byte 0 is 0x42 / d 01: 52 ; byte 1 is 0x52 / r 02: 62 ; byte 2 is 0x62 / D 03: 72 ; byte 3 is 0x72 / R 07: 1f ; bytes 03..0e are implicitly 00, byte 0f is 0x1f ~~~ and use it to generate an 8-byte file whose values (in hex form) would be: 42 52 62 72 00 00 00 1f The exact input format isn't carved in stone. I just picked ';' as a comment delimiter because it's a single character and unambiguous. The form of the offset: and 2-digit hex value just seemed obvious based on tradition. I suspect the ultimate solution involves using sed or awk to strip away the comment, then piping their output to xxd, but so far my first experiment has fallen flat on its face & I can't even get xxd to parse what ought to be a best-case simple text file. --- For my first attempt, I simplified config.src: ~~~ 00: 42 01: 52 02: 62 03: 72 ~~~ (omitting the comments and implied zero-bytes for now, and sticking to values corresponding to printable ASCII) ... then tried to generate the binary file from it: xxd -r config.src config.bin What I expected to see from cat config.bin and xxd config.bin: BRbr and 00000000: 42 52 62 72 BRbr What I ended up with: a 2-byte file with unprintable content cat can't render, and the following output from xxd config.bin: 00000000: 0301 So... problem #1... *What am I doing wrong with xxd*, and how can I fix it (or is there a better approach)? Keep in mind that I really want to specify one byte value per line, and would really like to be able to automatically skip sequential values and have them automatically filled with zeroes. The... problem #2... once I get xxd to parse my file, how can I go add the comments and strip them away before xxd sees them? Note that I'm not hellbent on using xxd per se... but this is a shared web server to which I don't have root or admin access, so apt-get install isn't an option, and compiling my own copies from source wouldn't necessarily be easy). --- *(Background info... not necessary essential to solving the problem, but adding context to why I'm trying to do it)* I'm working on an Arduino-based IoT controller. For the past few weeks, its configuration has consisted of hardcoded values and various interpretations of a DIP switch I've repurposed every few days. It's getting tedious. I'm not in the mood yet to implement a proper UI, so I came up with the idea of having it just fetch a binary config blob from my web server into a char[] as its first act upon starting up (enabling me to tweak runtime config values without having to go all the way and reflash the board itself, which is honestly kind of a pain at this point).
Bitbang3r (123 rep)
Apr 27, 2020, 12:39 AM • Last activity: Apr 27, 2020, 02:23 PM
2 votes
1 answers
3800 views
Make xxd display the the byte offset at the top column?
So Im using emacs which has a stupendous hexl-mode to view the byte offset in a file right over the hex values similar to: 87654321 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789abcdeff 00000000: 5765 6c63 6f6d 6520 746f 2047 4e55 2045 Welcome to GNU E As a fan of this capability. Wondering if t...
So Im using emacs which has a stupendous hexl-mode to view the byte offset in a file right over the hex values similar to: 87654321 0011 2233 4455 6677 8899 aabb ccdd eeff 0123456789abcdeff 00000000: 5765 6c63 6f6d 6520 746f 2047 4e55 2045 Welcome to GNU E As a fan of this capability. Wondering if this is a capability I can pull out of in the xxd or hexdump? Or if anybody has an awk script that does this and keeps it lined up properly
LFMekz (148 rep)
Jul 6, 2019, 12:21 PM • Last activity: Jul 6, 2019, 03:45 PM
2 votes
2 answers
7332 views
xxd with 64 bit addresses
I find xxd to be nice and convenient for simple manipulation of binary data in the terminal. However it has a limitation in that all addresses outputted by the program is limited to 32 bits. E.g. root:/# xxd -s 0x5baae0000 /dev/sda baae0000: 2d7c 6176 6976 6172 7c61 7469 7a61 720a -|avivar|atizar. N...
I find xxd to be nice and convenient for simple manipulation of binary data in the terminal. However it has a limitation in that all addresses outputted by the program is limited to 32 bits. E.g. root:/# xxd -s 0x5baae0000 /dev/sda baae0000: 2d7c 6176 6976 6172 7c61 7469 7a61 720a -|avivar|atizar. Notice the 5 at the beginning is gone. This means that using this directly with xxd -r will end up patching a region over 10 billion bytes earlier than what I want. In other words I want to e.g. do this, root:/# echo HELLO | xxd -o 0x5baae0000 | xxd -r - /dev/sdb but I end up having to work around this by doing something like this, root:/# echo HELLO | xxd -o 0x5baae0000 | xxd -r -seek 0x500000000 - /dev/sdb which adds back the missing 0x5000000 bytes offset into the target file when parsing the patch. --- In any case I want to know if there is either a unknown/clever way to have xxd use 64 bit addresses or an alternative program that operates like xxd without these limitations?
Christer (186 rep)
Jan 4, 2019, 08:30 PM • Last activity: Jan 31, 2019, 05:50 PM
Showing page 1 of 20 total questions