| | |
Hex conversion problem
![]() |
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
Any ideas???
Cheers
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:
Maybe? I dunno...
-G
If so, you could do something like:
Shell Scripting Syntax (Toggle Plain Text)
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 
Works with your example, and gives the same results. Hope this helps!
-G

Shell Scripting Syntax (Toggle Plain Text)
#!/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
Last edited by Gromit; Sep 5th, 2008 at 1:38 pm.
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 0
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
-b
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 0
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
As you can see i have my work cut out for me.
--Cheers
Blake
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
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

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
![]() |
Similar Threads
- Contactless Card Reader problem (VB.NET)
- can't find the problem after checking my similar programs (C++)
- number system conversion (C++)
- One's Compliment Dec to Hex Conversion (Assembly)
- Help with program to convert hex to dec (Assembly)
- FILE + conversion from dec to hex = I need help (C)
- Lite-On CD Burner problem (Storage)
Other Threads in the Shell Scripting Forum
- Previous Thread: hi, shell /awk for getting echo the linenumber from log file
- Next Thread: Dynamically-created shell commands
| Thread Tools | Search this Thread |





