Writing a sequence of bytes to a file
From ShawnReevesWiki
Jump to navigationJump to searchI wanted to be able to send every possible byte value through a serial port, one at a time, to test a receiver's reaction to each byte. There are many approaches to this, and I chose to create a file with each byte value, from zero through two hundred and fifty five. Here's how I made it.
Creating a file using echo and xxd
In any unix type computer, enter a bash terminal, and enter the following line of commands (split into three here for readability):
symbols=(0 1 2 3 4 5 6 7 8 9 A B C D E F); for i in "${symbols[@]}" ; do for j in "${symbols[@]}" ; do echo -n "$i$j "|xxd -r -p>>hextest.bin; done;done
An explanation of each command
- symbols=(...) creates an array called "symbols" from whatever appears between the spaces.
- for i in... creates a loop based on the list following.
- "${symbols[@]}" means return a list of each separate member of symbols, as a string. The quotes enclose any string, the dollar-sign indicates a variable follows, the at-symbol indicates that each member should be present but separate.
- do...done means to have whatever appears in between be repeated for every iteration of the loop.
- for j ... we put in so we have two digit hexadecimals. For every first digit, $i, we'll go through all possible second digits, $j.
- echo means output the following, -n meaning without line breaks.
- | means pipe the output of the previous command, echo, to the next command, xxd, instead of just showing the output on the terminal.
- xxd means translate the hexadecimal value, -r means revert the hexadecimal representation in ASCII, e.g. A3, to its actual byte value, e.g. 10100011. -p suppresses any extra formatting.