Hey all. I have an interesting problem. I have a file with a ton of raw hex data in it. I need to convert it into human readable text for display on a web site. The problem that I am having is this. For some of the data I need to change the order of the characters before running the conversion. For example:
A028: To convert switch the order of the bits (A028 -> 28A0), convert to decimal (10400), divide that number by 8 for the result (1300).
I was going to try to do it with PHP but figured that it might be easier with a shell script.

Any ideas???

Cheers

That's an interesting one... is the pattern for each one the same as your example?

If so, you could do something like:

TEST=(A028) ; echo -n ${TEST:2:4}${TEST:0:2}

Maybe? I dunno...

-G

Here's a test I wrote just to see if my math was right :)

#!/bin/bash

if [ -z "$1" ]
then
  echo "Usage: $0 <hex#>"
  exit
fi

TEST="$1"
HEX=$(echo -n ${TEST:2:4}${TEST:0:2})
#DEC=$(echo "ibase=16; obase=10; $HEX" |bc)
DEC=$(( 16#${HEX} ))
RESULT=$(( ${DEC}/8 ))

echo "Input was: $TEST"
echo "Rearranged to: $HEX"
echo "Converted to dec: $DEC"
echo "Divide by 8: $RESULT"

Works with your example, and gives the same results. Hope this helps!

-G

Awesome....thanks I'll give it a try. The data is actually raw j1939 data coming from a remote machine. this is the only operation that I wasn't really sure how to do. There are 3 or 4 parameters within the frame that need this conversion all the others are straight hex to decimal conversions and math probs. Thanks again for the help
-b

here's a link to a wiki page that i set up for quick reference. HERE
As you can see i have my work cut out for me.;)

--Cheers
Blake

ok so after much hair pulling, coffee and nicotene....here's where i'm at. I have a file that has a couple thousand lines which resemble the following:
FF17 1B 8 581B E001 0000 FFFF
FF15 1B 8 A028 3043 DB070000
FF16 1B 8 32 64 64 3A AD
What i need to do is convert that data to decimal. Unfortunately not all of it is just a simple conversion. The first three "Chunks" can be converted straight to decimal but the rest of it needs to have some other things done to it (i.e swap bytes and perform some math on each one). I am trying to use sed in conjunction with bc to do this but am running into a brick wall. for an example of what i need to do check out this link http://wiki.lormfg.com/index.php/Packet_Layout. Any help would be greatly appreciated.

P.S I was able to get this to work from the command line but with only one value as the input.

This is a really interesting project. I'd like to take a deeper look into it when I have some time :)

Although I love bash a lot, Perl has a lot of built in functions to handle the conversions you're trying to do. I'm not that good with Perl personally, but I know that some of what you're doing here might be a lot simpler in Perl. Just a thought :)

-G

In reference to this comment:

P.S I was able to get this to work from the command line but with only one value as the input.

Have you tried xargs? With maybe a loop that terminates at end of line/file? Just giving you another option.

I personally agree with Gromit about using PERL or some other language to do this as it is fairly complicated. Using BASH for quick and dirty or for prototyping is great, but you may start running into some limitations. I have tried to solve many an issue using shell scripting of various flavors, but sometimes it just can't do what you need to get done. =8)

Hope I don't get flamed for that. Just my personal opinion.

This article has been dead for over six months. Start a new discussion instead.