Hello,
With bash-script (ubunto server) I'm trying to read a binary file and, for each character, give back its ascii code (including extended ascii). For example:
HEX => ASCII => PRINT
f5 => 245 => �
50 => 80 => P
To load the binary file into a variable I tried in this way:
s=$(<"binaryfile.bin")
and for finding its length:
n=${#s}
However, $s does not seem to correspond to the actual content of the file and therefore also the length.
The exact number of characters I get in fact doing:
n=$(stat c%s "binaryfile.bin")
What I do not get is $s to match character for character the content of BinaryFile.
The equivalent vb6 works this way:
Open DataFile For Binary As #1
s = Input (LOF (1), 1)
n = len(s)
Close 1
For i = 1 To n
sn(i) = Asc(Mid(s, i, 1))
Next
I also tried to work with hexdump ...
hexdump -C binaryfile.bin
but I can not print the ascii code (extended) characters in the file.
If the variable $s contain the exact text of the file, I could use the following functions to get each character its ascii code (which is what I need).
<cut>
for(( i=0; i<=n; i++))
do
one_char=${s:i:1}
sn[$i]=$(ord "$one_char");
done
<cut>
#Asc() function
ord() {
LC_CTYPE=C printf '%d' "'$1"
}
#Chr() function
chr() {
[ "${1}" -lt 256 ] || return 1
printf \\$(printf '%03o' $1)
}
Any help?
Thank you
math