LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2008, 01:48 PM   #1
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Rep: Reputation: 15
Problem Putting Hex Doublets into Right Form


I'm attempting to take the following "16 Rows of Doublets:

Code:
f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00
00 00 00 00 00
00 00
00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00
00 00
d1 9c 3c 04 5f 30
59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00
And put it into this form:

Code:
f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00 00 00 00 00 00
00 00 00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00 00 00
d1 9c 3c 04 5f 30 59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00
From the first block of Hex, Rows 3 and 4 become one, Rows 5 and 6 become 1, 7 and 8 become 1, 9 and 10 become 1.

I'm looking for a simple solution in bash or perl If anyones got an idea.
 
Old 07-01-2008, 03:22 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
$ fmt -w 30 <  data.file
f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00 00 00 00 00 00
00 00 00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00 00 00
d1 9c 3c 04 5f 30 59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00
 
Old 07-01-2008, 03:42 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Wow, I just wrote this:
Code:
#!/bin/bash

n=1
while read line; do
    test "$line" || continue
    set $line
    while [ "$1" ]; do
        echo -n "$1"
        if [ $n -ge 10 ]; then
            n=0
            echo
        else
            echo -n " "
        fi
        let n++
        shift
    done
done </path/to/your/hex.file
..but bigearsbilly's solution is way more cooler!
 
Old 07-01-2008, 03:52 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
i get it right occasionally
 
Old 07-02-2008, 03:16 PM   #5
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly View Post
i get it right occasionally
I don't suppose you would know a way to remove the colums of white space from the stream of hex doublets?

As in, we start with this:

Code:
f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00 00 00 00 00 00
00 00 00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00 00 00
d1 9c 3c 04 5f 30 59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00

f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00 00 00 00 00 00
00 00 00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00 00 00
d1 9c 3c 04 5f 30 59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00
and we end up with this:

Code:
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000

f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000
I was using sed to search for a blank space and replace it with no space, but it has to be looped to work, and I'm concerned that the solution might remove the blank line between the paragraphs of HEX.

Any suggestions?
 
Old 07-02-2008, 03:47 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
sed doesn't need to be looped:

sed 's/ //g' input
 
Old 07-02-2008, 04:26 PM   #7
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by Mr. C. View Post
sed doesn't need to be looped:

sed 's/ //g' input
Mr. C. to the rescue! Thanks Again!
 
Old 07-02-2008, 04:35 PM   #8
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
sed doesn't need to be looped:

sed 's/ //g' input
What if I wanted to make the reverse true and take:

Code:
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000
and turn it into this:

Code:
f6 f6 f6 28 28 28 01 02 03 00
9c 3c 04 5f 30 59 ed ea e1 8c
8c 00 00 00 00 00 00 00 00 00
00 00 00 00 55 04 5f 30 59 ed
33 ff 37 00 00 00 00 00 00 00
d1 9c 3c 04 5f 30 59 ed ea e1
00 00 00 00 00 00 00 00 00 00
b3 71 aa d1 9c 3c 04 5f 30 59
00 00 00 00 00 00 00 00 00 00
 
Old 07-02-2008, 04:56 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You've been asking a whole series of questions regarding conversions back and forth between various data presentations. These sure sound like homework questions...
 
Old 07-02-2008, 05:33 PM   #10
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
You've been asking a whole series of questions regarding conversions back and forth between various data presentations. These sure sound like homework questions...
LOL.. No, I graduated a month ago with a M.S. Degree in Telecommunications Technologies..

I'm attempting to write a stream interpreter for a custom embedded linux AP. The idea is that the an encrypted hex stream will come to a management system and the management system will convert the hex to binary and put the binary into the right format, then decrypt it's simple encryption algorithm and using various byte values it will be able to check system status information. "It's like SNMP, but well... not!"

It's a pet project that I've wanted to work on for the longest time but I was busily writing my dissertation so I had no time. Now I've got a few months off before I start a new job and I felt like finishing this.

But no, not homework, Telecom doesn't require programming just an intro to linux course that I took 8 years ago "hence why I'm lost with a lot of this."
 
Old 07-02-2008, 05:46 PM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Ok, fair enough. I was mislead by the series of step-by-step questions, rather than the big picture; this is often a tip of a student cramming for homework due. My apologies.

sed 's/\(..\)/\1 /g; s/ $//'
 
Old 07-02-2008, 05:49 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
bash$ cat file.txt
f6f6f628282801020300
9c3c045f3059edeae18c
8c000000000000000000
0000000055045f3059ed
33ff3700000000000000
d19c3c045f3059edeae1
00000000000000000000
b371aad19c3c045f3059
00000000000000000000
Code:
bash$ sed 's/\(..\)/\1 /g' file.txt
f6 f6 f6 28 28 28 01 02 03 00 
9c 3c 04 5f 30 59 ed ea e1 8c 
8c 00 00 00 00 00 00 00 00 00 
00 00 00 00 55 04 5f 30 59 ed 
33 ff 37 00 00 00 00 00 00 00 
d1 9c 3c 04 5f 30 59 ed ea e1 
00 00 00 00 00 00 00 00 00 00 
b3 71 aa d1 9c 3c 04 5f 30 59 
00 00 00 00 00 00 00 00 00 00
 
Old 07-02-2008, 06:04 PM   #13
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Original Poster
Rep: Reputation: 15
I love it when I get two identical responses at the same time, it bring me confidence in the forum.

Mr. C. ... sorry for the step by steps, I may know the big picture but coming up with a procedure for getting my result has been a trip. I've got it down now though I think.

The basic idea is:

1.) Data from Open Source AP Streams in, in Hex
2.) I strip that data down to just the components I want
3.) Convert the remaining hex in dublets to binary
4.) (And this is next xor the binary cyphertext against a key
5.) Convert the resultant binary back into hex doublets
6.) Compare the doublets to known values of up/down, temp, cpu load, mem load.
7.) Make a nice front end using dialog or zenity

It's taken my this week basically to figure out the process as I play with perl for the first time.

Thanks for all your help.
 
Old 07-02-2008, 06:13 PM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
No worries. Sounds like you're on track. Most likely, some of the steps can be collapsed into a single filter. But you have the pieces now.
 
Old 07-07-2008, 12:43 PM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
perl?

use the pack unpack functions for the translations
here are some silly toys I made to play with

hex_read
Code:
#!/usr/local/bin/perl -n

tr/0-9a-fA-F//cd;
print pack "H*" , $_;
hex_write
Code:
#!/usr/local/bin/perl -ns
BEGIN {
    warn "-chomp to ignore new line characters\n" unless $chomp;
    warn "-neat for nice easier spacing\
	(hint: finish off with 'fmt' command)\n" unless $neat;
	warn "\n";

}
chomp if $chomp;
@L =  unpack("H2" x length($_), $_) ;
print $neat? "@L\n":@L;
binary_read
Code:
#!/bin/perl -n

tr/01//cd;
print pack "B*" , $_;
binary_write
Code:
#!/bin/perl -ns
BEGIN {
    warn "-chomp to ignore new line characters\n" unless $chomp;
    warn "-neat for nice easier spacing\
	(hint: finish off with 'fmt' command)\n" unless $neat;
}
chomp if $chomp;
@L = unpack "B8" x length($_), $_ ;

print $neat? "@L\n":@L;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
PHP: build query from form entry, then display results in the same form tonedeaf1969 Programming 4 06-22-2007 07:55 AM
Hex code problem! applee Programming 9 03-30-2007 04:58 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 10:27 AM
how do I copy a whoel folder form one directory to another form the command line? zwyrbla Linux - Newbie 8 08-24-2004 06:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration