This question was out there for a while and I thought I should offer some bonus points if I can get it to work.
Recently at work, I wrote a par
To change encoding from File3 to File1, you use a script like this:
#!/bin/bash
# file name: tobin.sh
fileName="tobin.txt" # todo: pass it as parameter
# or prepare it to be used via the pipe...
while read line; do
for hexValue in $line; do
echo -n -e "\x$hexValue"
done
done < $fileName
Or, if you just want to pipe it, and use like the xxd example in this thread:
#!/bin/bash
# file name: tobin.sh
# usage: cat file3.txt | ./tobin.sh > file1.bin
while read line; do
for hexValue in $line; do
echo -n -e "\x$hexValue"
done
done
If you really want to use BASH for this, then I suggest you start using array to nicely build your packet. Here is starting code:
#!/bin/sh
# We assume the script will run on a LSB architecture.
hexDump() {
for idx in $(seq 0 ${#buffer[@]}); do
printf "%02X", ${buffer[$idx]}
done
} # hexDump() function
###
# dump() dumps the current content of the buffer[] array to the STDOUT.
#
dump() {
# or, use $ptr here...
for idx in $(seq 0 ${#buffer[@]}); do
printf "%c" ${buffer[$idx]}
done
} # dump() function
# Beginning of DB Package Identifier: ==
buffer[0]=$'\x3d' # =
buffer[1]=$'\x3d' # =
size=2
# Total Package Length: 2
# We start with 2, and later on we update it once we know the exact size...
# Assuming 32bit architecture, LSB, this is how we encode number 2 (that is our current size of the packet)
buffer[2]=$'\x02'
buffer[3]=$'\x00'
buffer[4]=$'\x00'
buffer[5]=$'\x00'
# Offset to Data Record Count field: 115
# I assume this is also a 32bit field of unsigned int type
ptr=5
buffer[++ptr]=$'\x73' # 115
buffer[++ptr]=$'\x00'
buffer[++ptr]=$'\x00'
buffer[++ptr]=$'\x00'
#hexDump
dump
Output:
$ ./tobin2.sh | hexdump -C
00000000 3d 3d 02 00 00 00 73 00 00 00 00 |==....s....|
0000000b
Sure, this is not solution the the original post... The solution will use something like this to generate binary output. The biggest problem is that we still do not know the types of fields in the packet. We also do not know the architecture (is it bigendian, or littleendian, is it 32bit, or 64bit). You must give us the specification. For an instance, the lenght of the package is of what type? We do not know that from that TXT file!
In order to help you do what you have to do, you must find us the specification about sizes of those fields.
Note it is a good start though. You need to implement convenience functions to, for an example, automatically fill the buffer[] with values from a string encoded with hex values. So you can do something like write $offset "ff c0 d3 ba be"
.