niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
HexBinDecode : Some feedback on the script please
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
MyUsenetAccount
*nix forums beginner


Joined: 06 May 2006
Posts: 3

PostPosted: Wed Jul 19, 2006 2:32 pm    Post subject: HexBinDecode : Some feedback on the script please Reply with quote

I'd appreciate some feedback on the script below. I've limited
scripting experience but I find that it is very easy to get a script to
do the job I want but normally it is not the most efficient way.
Therefore do you have some advice on the script? What could be improved
and done cleaner/more efficient etc...?

The script works as intended. It asks the user for 5 hex bytes (no
spaces between the bytes and in upper case). It then converts this into
one long bit stream. Finally the script translates/decodes different
positions of the bit stream into some user friendly format. One problem
i have is with leading zeros being stripped from the hex->bin
conversation.

Many Thanks

#!/bin/bash
#set -x

DebugInput="8102C4C004"

echo ""
echo ""
echo "Type the data. Must be hex in upper case, 5 bytes without
spaces"

Msg_To_Translate=$DebugInput
read Msg_To_Translate #comment for automatic input. Debug Test
only

#should add functionality here to make lower case->upper case

hex=$Msg_To_Translate
binall=`echo 2o16i${hex}p|dc`
#echo binall $binall

#is it possible to perserve the leading zeros here?
#If 0x41 is the 1st byte then the bin translation is 1000001 (missing
leading zero)

#need leading zeros for 1st byte so calculate how many characters are
in the 1st byte
#and add them to the variable
#!!! Doesn't work correcly for leading characters if the 1st byte is
0x00
#the leading zeros from the 2nd byte are then missing.

byte1=${hex%????????}
byte1=`echo 2o16i${byte1}p|dc`

Length=`echo ${#byte1}`
ToAdd=$((8-$Length))
#The number in ToAdd is the number of 0's to add

#need a better method than below to add the leading spaces at the
start of the byte
#something like a shift

if [ $ToAdd -ne 0 ] #only add padding if leading zeros are missing
then

case "$ToAdd"
in
1 )
binall="0"$binall ;;
2 )
binall="00"$binall ;;
3 )
binall="000"$binall ;;
4)
binall="0000"$binall ;;
5)
binall="00000"$binall ;;
6)
binall="000000"$binall ;;
7)
binall="0000000"$binall ;;
*)
echo "Invalid 1st byte"
;;
esac

fi

#the next section performs the decoding of the various bit patterns in
the binary stram

special_bit=`echo ${binall:0:1}` #special_bit is the MSB of the 1st
byte

foo=`echo ${binall:3:5}`
foo=`echo 10o2i${foo}p|dc`

foob=`echo ${binall:11:4}`
foob=`echo 10o2i${foob}p|dc`

#.....
#.....

#print out the decoded data
echo ""
echo "special_bit is $special_bit"
echo "foo is $foo"
echo "foob is $foob"
echo ""

exit 0
Back to top
Kenan Kalajdzic
*nix forums beginner


Joined: 29 Apr 2006
Posts: 31

PostPosted: Thu Jul 20, 2006 9:01 am    Post subject: Re: HexBinDecode : Some feedback on the script please Reply with quote

MyUsenetAccount <myusenetaccount@yahoo.co.uk> wrote:
Quote:

...

The script works as intended. It asks the user for 5 hex bytes (no
spaces between the bytes and in upper case). It then converts this into
one long bit stream. Finally the script translates/decodes different
positions of the bit stream into some user friendly format. One problem
i have is with leading zeros being stripped from the hex->bin
conversation.

#!/bin/bash
#set -x

DebugInput="8102C4C004"

echo ""
echo ""
echo "Type the data. Must be hex in upper case, 5 bytes without
spaces"

Msg_To_Translate=$DebugInput
read Msg_To_Translate #comment for automatic input. Debug Test
only

#should add functionality here to make lower case->upper case

Msg_To_Translate=`echo "$Msg_To_Translate" | tr '[a-f]' '[A-F]'`

Quote:
hex=$Msg_To_Translate
binall=`echo 2o16i${hex}p|dc`
#echo binall $binall

#is it possible to perserve the leading zeros here?
#If 0x41 is the 1st byte then the bin translation is 1000001 (missing
leading zero)

#need leading zeros for 1st byte so calculate how many characters are
in the 1st byte
#and add them to the variable
#!!! Doesn't work correcly for leading characters if the 1st byte is
0x00
#the leading zeros from the 2nd byte are then missing.

byte1=${hex%????????}
byte1=`echo 2o16i${byte1}p|dc`

Length=`echo ${#byte1}`
ToAdd=$((8-$Length))
#The number in ToAdd is the number of 0's to add

#need a better method than below to add the leading spaces at the
start of the byte
#something like a shift

if [ $ToAdd -ne 0 ] #only add padding if leading zeros are missing
then

case "$ToAdd"
in
1 )
binall="0"$binall ;;
2 )
binall="00"$binall ;;
3 )
binall="000"$binall ;;
4)
binall="0000"$binall ;;
5)
binall="00000"$binall ;;
6)
binall="000000"$binall ;;
7)
binall="0000000"$binall ;;
*)
echo "Invalid 1st byte"
;;
esac

fi

Adding leading zeros is accomplished easily with printf. You treat
a binary number as an (unsigned) integer:

byte1=`printf "%08u" $byte1`

--
Kenan Kalajdzic
Back to top
joe@invalid.address
*nix forums Guru Wannabe


Joined: 16 Jul 2005
Posts: 139

PostPosted: Thu Jul 20, 2006 2:41 pm    Post subject: Re: HexBinDecode : Some feedback on the script please Reply with quote

"MyUsenetAccount" <myusenetaccount@yahoo.co.uk> writes:

Quote:
I'd appreciate some feedback on the script below. I've limited
scripting experience but I find that it is very easy to get a script to
do the job I want but normally it is not the most efficient way.
Therefore do you have some advice on the script? What could be improved
and done cleaner/more efficient etc...?

The script works as intended. It asks the user for 5 hex bytes (no
spaces between the bytes and in upper case). It then converts this into
one long bit stream. Finally the script translates/decodes different
positions of the bit stream into some user friendly format. One problem
i have is with leading zeros being stripped from the hex->bin
conversation.

Many Thanks

#!/bin/bash
#set -x

DebugInput="8102C4C004"

echo ""
echo ""
echo "Type the data. Must be hex in upper case, 5 bytes without
spaces"

Msg_To_Translate=$DebugInput
read Msg_To_Translate #comment for automatic input. Debug Test
only

#should add functionality here to make lower case->upper case

hex=$Msg_To_Translate
binall=`echo 2o16i${hex}p|dc`
#echo binall $binall

#is it possible to perserve the leading zeros here?
#If 0x41 is the 1st byte then the bin translation is 1000001 (missing
leading zero)

#need leading zeros for 1st byte so calculate how many characters are
in the 1st byte
#and add them to the variable
#!!! Doesn't work correcly for leading characters if the 1st byte is
0x00

I would setup a function to return the binary string. In the function
check the length of the input hex number and make sure the binary
number is 8 times that:

hex2bin()
{
hex=${1#0x}
len=${#hex}
binlen=$(( $len * 4 ))
bin=`echo 2o16i${hex}p|dc`
while [[ ${#bin} -lt $binlen ]];do
bin="0$bin"
done
echo $bin
}

Joe
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Mon Dec 01, 2008 8:18 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts does squid 2.6 support setting cache_peer port in redirec... Victor Tsang Squid 0 Fri Jul 21, 2006 8:16 am
No new posts mail script eeb4u@hotmail.com shell 3 Fri Jul 21, 2006 5:50 am
No new posts A simple bash script JPB Suse 2 Fri Jul 21, 2006 2:19 am
No new posts Getting started in PXPerl, i.e how to run a script? Markus Hänchen Perl 5 Thu Jul 20, 2006 2:12 pm
No new posts Match pattern in ksh script lnrntx@gmail.com shell 6 Thu Jul 20, 2006 1:48 am

WesternUnion | Car Credit | WoW Gold | Web Advertising | Buy Anything On eBay
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2052s ][ Queries: 16 (0.1197s) ][ GZIP on - Debug on ]