ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

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.

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.

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.

>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.

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.

>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.

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:

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.

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.

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.

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.

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])

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(@_);
}

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.