Hex conversion problem

Reply

Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Hex conversion problem

 
0
  #1
Sep 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Hex conversion problem

 
0
  #2
Sep 5th, 2008
That's an interesting one... is the pattern for each one the same as your example?

If so, you could do something like:

Shell Scripting Syntax (Toggle Plain Text)
  1. TEST=(A028) ; echo -n ${TEST:2:4}${TEST:0:2}

Maybe? I dunno...

-G
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Hex conversion problem

 
0
  #3
Sep 5th, 2008
Here's a test I wrote just to see if my math was right

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. if [ -z "$1" ]
  4. then
  5. echo "Usage: $0 <hex#>"
  6. exit
  7. fi
  8.  
  9. TEST="$1"
  10. HEX=$(echo -n ${TEST:2:4}${TEST:0:2})
  11. #DEC=$(echo "ibase=16; obase=10; $HEX" |bc)
  12. DEC=$(( 16#${HEX} ))
  13. RESULT=$(( ${DEC}/8 ))
  14.  
  15. echo "Input was: $TEST"
  16. echo "Rearranged to: $HEX"
  17. echo "Converted to dec: $DEC"
  18. echo "Divide by 8: $RESULT"

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

-G
Last edited by Gromit; Sep 5th, 2008 at 1:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Re: Hex conversion problem

 
0
  #4
Sep 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Re: Hex conversion problem

 
0
  #5
Sep 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Hex conversion problem

 
0
  #6
Sep 5th, 2008
Nice! Looks like fun
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Re: Hex conversion problem

 
0
  #7
Sep 5th, 2008
should be interesting
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Re: Hex conversion problem

 
0
  #8
Sep 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: Hex conversion problem

 
0
  #9
Sep 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: lmrd is an unknown quantity at this point 
Solved Threads: 0
lmrd lmrd is offline Offline
Newbie Poster

Re: Hex conversion problem

 
0
  #10
Sep 22nd, 2008
cool...i'll give it a try...thx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC