LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-04-2008, 05:57 PM   #1
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Rep: Reputation: 16
Hex output of a hex/ascii input string


Updated to better explain;

Maybe it's time to start my own thread on this .

What I need is something I've not explained correctly so it's time to start over.

I need to take a string of characters, say T12b3R4n, and get all of the possible 0-9 and A-F permutations I can out of the string, in the order it is entered.

So, using the string above as an example, T12b3R4n;

-No need for random combinations. The string is entered in the order that the output would be required in.

-The result I need are permutations of 0-9/A-F only. This means that T only outputs as 54 for example.

-The permutations I need start at 1 for example.
The char 1 could be either a 1 or 31. (not 0x31).
The char 2 could be a 2 or it could be a 32.
Same for the b, 3 and 4 using my example above.

-The char n would output only as 6E.

-The final output can only be 0-9/A-F and 14 and 32 chars. So, all combinations within the range.

-Last, so that it's more easily readable, an output of 4 digits, a dash, 4 more digits, a dash and only in CAPS.

I'm not a programmer and have been trying to do this for weeks but just can't so I seek help from you kind folks.
Programming is simply not something I can do or take on so am asking for an actual perl (or other Linux) script which will do this.

I've had folks send me insanely tiny code to do similar things so I thought I would ask the brains around here. Very much appreciated if you can show me code which can do this.

Thank you very much!

Last edited by mlewis; 04-05-2008 at 10:01 AM.
 
Old 04-04-2008, 06:23 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You want all permutations of "123b4"?
Or do you want to know how many permutations there are?

Code:
No need for all combinations which include random orders of the input string.
That pretty much defines what a permutation is. So what do you want?

Code:
The char 1 could be either a 1 or 31.
The string "31" is two characters.

I'm not certain what you want, or of what use this would be.
You seem to be confusing hexidecimal ascii text with what it represents.
 
Old 04-04-2008, 08:04 PM   #3
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
It is a little hard to explain when you're not a programmer, I apologize.

>You want all permutations of "123b4"?
>Or do you want to know how many permutations there are?

I want all of the permutations.

>>No need for all combinations which include random orders of the input >>string.

>That pretty much defines what a permutation is. So what do you want?

What I mean by this is that I don't need permutations which include the input string out of the order it's been input as. In other words, just in that order, not permutations which would include the input string in other orders.

>>The char 1 could be either a 1 or 31.

>The string "31" is two characters.

Right but if you output all of the possible permutations, then 1 could be a one or it could be a 31.

>You seem to be confusing hexidecimal ascii text with what it represents.

No, I'm just having a difficult time explaining it .
To me, and I'm probably wrong on the term, hex characters are 0-9 and A-F. If that is not hex characters, then I'm not explaining this correctly.

The end result however, would still be a string which uses only 0-9 and A-F characters.

Mike
 
Old 04-04-2008, 08:43 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Let me see if I understand what you want:

You start with a string of ASCII characters, say "T12b3R4n"
Some of the characters in the string might represent themselves, but, on the other hand, they might be a literal hexadecimal character.

You want to find all possible hexadecimal representation of the input string.

So, try this:

1) Create a two-dimensional array containing, in the first column, the hex representation of the input character and, in the second, either a special n/a code or the actual character if it was a valid hex character.
2) Build all possible hex strings by a nested loop. The first string would be the contents of the first column. The second would be the first column with the first entry where the second column value is not the n/a symbol replacing the value in the first column. And so on.

The total number of possible strings would be 2 raised to the power n, where n is the number of entries in the second column which are not the n/a symbol.

So, does that sound right?
 
Old 04-04-2008, 09:06 PM   #5
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
>You start with a string of ASCII characters, say "T12b3R4n"
>Some of the characters in the string might represent themselves, but,
>on the other hand, they might be a literal hexadecimal character.

Some of it is known though. For example, A-F are known so those are included in the permutation of it's digit and it's ascii value.
Those such as T are beyond A-F so those are for sure their ascii value.
So, T=54, R=52 and n=6E are their given value in the output string, and in the order which they show up. As you call it, those represent themselves but in their ascii value.

So, the output definitely starts with 54, there would be 52 in the 6th position and 6E would be the end of the output. Those are givens which the program has to account for.

The permutations would be only for 1 which can be 1 or 31, 2 which can be 2 or 32, b which could be b or 62, 3 which could be 3 or 33 and 4 which could be 4 or 64.

>You want to find all possible hexadecimal representation of the input >string.

The output, can only be between 14 and 32 characters long using 0-9 and A-F.

>So, try this:

Wish I knew how to 'try this' . That's why I said I'm not a programmers so am asking for help.

>1) Create a two-dimensional array containing, in the first column,
>the hex representation of the input character and, in the second, >either a special n/a code or the actual character if it was a valid
>hex character.

I actually started doing that by hand just to get a sense of what it looks like.

>The total number of possible strings would be 2 raised to the power n, >where n is the number of entries in the second column which are >not the n/a symbol.

If I was randomizing the input string but I'm not so would it really be that long? Sure, it'll be many combinations but not as many as randomizing the string.
I mean, there are known factors but it's possible I'm just not explaining it right yet.

The known factors are the 1 can only be 1 or it's ascii value.
The output can only be 0-9 and A-F and 14 to 32 characters max.
I keep calling 0-9 and A-F hex but obviously, there's another term for those, right?

It's basically just showing me what the combinations using the digit or it's ascii value within the 14-32 digit limit would be, along with the fact that the output can't be anything but 0-9 and A-F.

Mike
 
Old 04-05-2008, 12:18 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
As I understand it each input character will have one or two outputs. If it is between G-Z, then that will result in the ASCII value as a hex number. Otherwise it will have two possible output values, either the ASCII value as a hex number or its actual value. This means that there will be no more than n squared possible output strings where n is the number of input characters. Which is less than 2 to the power n but it can still get quite large.
 
Old 04-05-2008, 12:35 AM   #7
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
>As I understand it each input character will have one or two outputs.

Correct. Anything which is A-F would be left as it is.

>If it is between G-Z, then that will result in the ASCII value as a hex >number.

Correct, G-Z would always be their 2 digit equivalent.

>Otherwise it will have two possible output values, either the ASCII value >as a hex number or its actual value.

Correct. If I'm explaining this correctly. If I could see it run, I'll better understand how to explain it.

>This means that there will be no more than n squared possible output >strings where n is the number of input characters.

I'm not sure about this because there are the other variables which are that it would not be smaller than 14 or larger than 32 characters total using 0-9/A-F. It would start with and end with a character within G-Z so the start and end numbers would also be known.

You probably know more than I about that one.

Mike
 
Old 04-05-2008, 05:32 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
I am of course wrong and the exact number of output strings would be 2 to the m where m equals the number of characters that are potential hex characters, as PTrenholme stated. To clarify this I'll use your example string T12b3R4n, this has five potential hex characters 12b34 thus there will be 2^5 or 32 output strings, as follows:

5412b35246E

54312b35246E

54132b35246E
543132b35246E

54126235246E
543126235246E
541326235246E
5431326235246E

5412b335246E
54312b335246E
54132b335246E
543132b335246E
541262335246E
5431262335246E
5413262335246E
54313262335246E

5412b352346E
54312b352346E
54132b352346E
543132b352346E
541262352346E
5431262352346E
5413262352346E
54313262352346E
5412b3352346E
54312b3352346E
54132b3352346E
543132b3352346E
5412623352346E
54312623352346E
54132623352346E
543132623352346E

By writing out the expected output that serves as a check that the problem is understood - thus if this is not the output that you want then I don't understand the problem - and secondly it can help to lead to a solution. In this case the problem can be looked at as a recursive one and the solution is then fairly trivial.
 
Old 04-05-2008, 08:21 AM   #9
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
Unfortunately, I suspect that my explanations are complicating this a lot more than it should be. It's really pretty simple, it's just hard to explain in writing . Here is possibly a better example.

A 1 could either be the ascii 0x31 or the ascii 0x01.
But in human readable form as in 1 or 31, no 0x.

Basically, the input can be any ascii hex character.

The givens are that the output can only be 0-9 and A-F and between 14 to 32 characters of permutation.

The input would also have a \ separator, used as part of the input to allow the program to determine permutations.
In other words, it might not always be 4 digit outputs, I might want to have an output of AD04-F5E33F-DD3S-12-FDRE for example. The program would allow me to move separators around for various output.

An example below.

The output can only be segments of 4 digits of 0-9/A-F. It would be good if the program allowed the separator to change however, say allowing 6/8 digit segments but that's not too important.

So, here is my example which I hope will de-complicate the things I've said .

Basically, a little program that would take an input something like; Q5\fcS\cLm\cdN for example.

The program would be told/know that the start and end are exactly what they are so no need to work on those.

So using the above example, starting with Q5. Since only 4 digits can fit into the segments, then Q=51, 5=35.
Same for the end of my example, cdN can only equal CD4E.

Of course, anywhere else, c, and d, would be part of the permutations.

So, now I know so far that the above is 5135-xxxx-xxxx-CD4E.

So I would be wanting 4 digit permutations for fcS\cLm with an output of 0-9/A-F. So for the first xxxx section, with the slash being where I have it in this case, the output would probably be FC53 unless there is another combination I can't see.

The cLm however could be 634C6D or C4C6D.

The program is really just looking at an input with separators, it doesn't need to jumble anything up, it just needs to figure out all of the possible permutations of 0-9 and A-F since I can't possibly do that in my head or on paper.

I hope this better explains .

Mike

Last edited by mlewis; 04-05-2008 at 08:28 AM.
 
Old 04-05-2008, 08:47 AM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Mike, please explain why the worked out example presented by graemef, above, does not satisfy your requirements. (Except for the "separators," which appear to me to be a distraction of little value in the problem.)

Perhaps you could go through each of the 32 example results (after adding your "separators" to the sample input you provided) and explain, in each case, why the result does not satisfy your requirements.
 
Old 04-05-2008, 09:01 AM   #11
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by PTrenholme View Post
Mike, please explain why the worked out example presented by graemef, above, does not satisfy your requirements. (Except for the "separators," which appear to me to be a distraction of little value in the problem.)

Perhaps you could go through each of the 32 example results (after adding your "separators" to the sample input you provided) and explain, in each case, why the result does not satisfy your requirements.
Maybe it does satisfy the requirements, I just wanted to add more information because I feel that I am not very well able to explain this correctly.

I might understand it better when I was able to enter some inputs of my own and see the results. Then I could ask for something or clarify.

The separators aren't important, they just help to see things better. More than anything, I just need all of the permutations of the input.

Mike

Last edited by mlewis; 04-05-2008 at 09:13 AM.
 
Old 04-05-2008, 04:27 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,399

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
Quote:
Originally Posted by mlewis
it doesn't need to jumble anything up, it just needs to figure out all of the possible permutations
You should probably lookup the definition of permutation...

Anyways here is some Python code that I think does something like what you want:
Code:
#!/usr/bin/env python
import string

def hexstr(letter):
    return hex(ord(letter))[2:]

def possibilities (letter):
    if letter in string.hexdigits:
        return [letter, hexstr(letter)]
    else:
        return [hexstr(letter)]

def mklist(combos, acc=['']):
    if len(combos) == 0:
        return acc
    else:
        next_acc = []
        for poss in combos[0]:
            for str in acc:
                next_acc.append(str+poss)
        return mklist(combos[1:], next_acc)

def combos (string):
    return mklist([possibilities(letter) for letter in string])
Sample usage:
Code:
>>> combos("cdN")
['cd4e', '63d4e', 'c644e', '63644e']
>>> combos("Q5")
['515', '5135']
>>> combos("cLm")
['c4c6d', '634c6d']
>>> filter( lambda s: len(s) == 4, combos("cdN"))
['cd4e']
>>> filter( lambda s: len(s) == 4, combos("Q5"))
['5135']
>>> filter( lambda s: len(s) == 4, combos("cLm"))
[]
 
Old 04-05-2008, 04:45 PM   #13
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
>You should probably lookup the definition of permutation...

Oh my god, you're right. I so badly need to get some sleep. Not permutations then but combinations of the string in the order it is entered.


I'll give this a try and post my findings. thanks very much.

Mike
 
Old 04-05-2008, 07:37 PM   #14
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by mlewis View Post
Maybe it does satisfy the requirements
If all you want is graemef’s output, you can use perl as well. What comes in handy here is the Schwartzian Permutator with an appropriate input.

For example, to get similar output to that of graemef (with alternate order) you might do:
Code:
#!/usr/bin/perl -w

use strict;

sub permute;
sub hexstr;

my $input = "T12b3R4n";

print @$_,"\n" for permute
map {
	# upper case hex chars map to themselves
	/[A-F]/ && [$_] ||
	# other hex chars map to themselves or to hex rep. of their ASCII value
	/[a-f0-9]/ && [$_, hexstr($_)] ||
	# all other chars map to the hex rep. of their ASCII value
	[hexstr($_)]
} split//, $input;

sub hexstr { return uc unpack "H2", $_ }

sub permute {
	my $last = pop @_;
	return map [$_], @$last unless (@_);
	return map { my $left = $_; map [@$left, $_], @$last } permute(@_);
}

Last edited by osor; 04-05-2008 at 09:07 PM.
 
Old 04-06-2008, 12:08 AM   #15
mlewis
Member
 
Registered: Mar 2006
Posts: 172

Original Poster
Rep: Reputation: 16
>If all you want is graemef’s output, you can use perl as well. What >comes in handy here is the

What a cool surprise, thanks so much guys.
Is there a way I can have something entered at the start of each line, such as a >>> then the output digit?

I'll give this a try tomorrow and reply.

Mike

Last edited by mlewis; 04-06-2008 at 12:47 AM.
 
  


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
[bash] ASCII to HEX and hex to ascii ////// Programming 15 03-20-2014 01:23 PM
hex to ascii and ascii to hex ilnli Programming 7 08-31-2007 11:55 AM
display in hex + perl + non ASCII characters kshkid Programming 4 02-06-2007 04:48 PM
How do i write to a serial port (modem) in ascii or hex directly? Taliesin.duchaos Linux - Networking 6 04-21-2006 08:31 AM
Binary to Hex Ascii converter carboncopy Slackware 1 05-28-2004 09:09 AM


All times are GMT -5. The time now is 11:56 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration