# !/bin/sh## Bin2Hex2Dec.sh## A DEMO to show how to display the contents of a binary file onto the screen and convert any single one of# those hexadecimal contents to decimal, retrieve the decimal string and use it ii a simple addition of the# retrieved decimal string with a fixed number to give a result. The idea was to be able to get a byte value# from say an external source and use it inside a bash script, in this case a value stored on the HDD.## This DEMO was only designed for a Macbook Pro 13 inch, OSX 10.7.5 using the default, (BASH), Terminal.# It has NOT been tested on any other platform but I suspect other Linux and UNIX flavours might also work.## To run use the usual method for launching an _executable_ from its /directory/drawer/folder:-## xxxxx$ ./Bin2Hex2Dec.sh<CR>## And away you go...## This script will need to made executable using the command "chmod" and will save and load files to YOUR# default /directory/drawer/folder. The files saved to the HDD will be named:-# BinaryString.dat# BinaryString.txt# # $VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.# This is Public Domain and you may do with it as you please. Ignore the (C) inside the code...

# Set up a simple user screen/window...clearprintf "\n\$VER: Bin2Hex2Dec.sh_Version_0.00.10_Public_Domain_B.Walker_G0LCU.\n"printf "\nGenerate and save a binary file, display a hexadecimal dump of that file,\n"printf "select a single byte from this binary file, display as adecimal number in\n"printf "string format then add this string number to another number...\n\n"

# NOTE:- Double back slashes required to ensure binary is saved rather than the string.binstr="\\x00\\x07\\x0A\\x0D\\x7F(C)2012, B.Walker, G0LCU.\\x80\\xFF"

# Do a text hexadecimal dump to the screen only of the 32 byte string...hexdump -C BinaryString.dat

# Just an offset is needed for a single byte with a "subsript" offset inside the range of 0 to 31 for this 32 byte dump.# The first byte is chosen for this DEMO. Just change this value to anything between 0 and 31 inclusive.subscript=0

# Now select this single byte and save the value as a decimal string.hexdump -n1 -s$subscript -v -e '1/1 "%u"' BinaryString.dat > BinaryString.txt

# Allow time for grabbed decimal byte value string to settle.sleep 1

# The "subscript" offset is set at the first byte in the string, value zero, (0)...printf "\nNow obtain the _byte_ from offset 0, the first byte, (0x00), single\n"printf "byte length, already converted to a decimal string from hexadecimal...\n\n"

# Now retrieve the decimal byte string for further use from the file "BinaryString.txt".read somedata < "BinaryString.txt"

# Now manipulate the retrieved string by adding a number to it...printf "Decimal value in string format = $somedata...\n\n"printf "Now ADD a number 7 to the decoded decimal string. $somedata + 7 = "$(($somedata+7))"...\n\n"printf "(Now edit the script and change the _variable_ ~subscript~ to another value.)\n\n"

Generate and save a binary file, display a hexadecimal dump of that file,select a single byte from this binary file, display as adecimal number instring format then add this string number to another number...

Generate and save a binary file, display a hexadecimal dump of that file,select a single byte from this binary file, display as adecimal number instring format then add this string number to another number...

Generate and save a binary file, display a hexadecimal dump of that file,select a single byte from this binary file, display as adecimal number instring format then add this string number to another number...

Generate and save a binary file, display a hexadecimal dump of that file,select a single byte from this binary file, display as adecimal number instring format then add this string number to another number...

Bazza wrote:Please give you Linux flavour and the Shell/Terminal you use...

It shouldn't matter which distro you use, nor which terminal, because /bin/sh calls your shell program in POSIX compliant mode. That's about as portable as it gets. You really shouldn't specify bash for a script unless you actually need bash extras, like arrays.

"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)

Remember, I was using only a Macbook Pro and the default shell is BASH.

I hadn't even noticed the BASH part though, as the default shell for the many flavours of Linux I have used, IIRC, have been BASHes...

If I am wrong then I stand corrected...

However, towy's setup did NOT work so my code is NOT fully portable. So it is either my code that is wrong or towy's shell/terminal is doing something wrong. Either way something is not correct and I have no idea why the DAT file is NOT being saved as a binary file but instead it is a text file...

Bazza wrote:Remember, I was using only a Macbook Pro and the default shell is BASH.

I hadn't even noticed the BASH part though, as the default shell for the many flavours of Linux I have used, IIRC, have been BASHes...

The default shell is irrelevant because you have specified /bin/sh in the shebang line. Although this is often a symlink to /bin/bash, bash behaves as a pure POSIX shell when called like this, not as bash. So your script is a shell script, not a bash script.

Having said that, I tried running it with sh, bash and zsh abd the result was always the same. I think towy is just being difficult

"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)