Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (3) </ 1 [2] 3 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: Convert ascii wifi key to hex< Next Oldest | Next Newest >
curaga Offline





Group: Members
Posts: 2163
Joined: Feb. 2007
Posted: Sep. 18 2007,14:51 QUOTE

Yes. I can also think for situations where you would want to check if it's still correct, without going to the browser, the router config page, logging in etc and then using this in asci2hex way..

--------------
There's no such thing as life. Those mean little jocks invented it ;)
-
Windows is not a virus. A virus does something!
Back to top
Profile PM 
curaga Offline





Group: Members
Posts: 2163
Joined: Feb. 2007
Posted: Sep. 18 2007,14:53 QUOTE

Some programs give the key with the dash after every four characters, some with colon after every two. It doesn't really matter, as they are just there to ease reading, and if you happen to give the "wrong" one, the program will just convert it to the form it likes better.

--------------
There's no such thing as life. Those mean little jocks invented it ;)
-
Windows is not a virus. A virus does something!
Back to top
Profile PM 
WDef Offline





Group: Members
Posts: 798
Joined: Sep. 2005
Posted: Sep. 18 2007,14:58 QUOTE

That clarifies that.
Back to top
Profile PM 
WDef Offline





Group: Members
Posts: 798
Joined: Sep. 2005
Posted: Sep. 18 2007,15:40 QUOTE

Curaga, here's quick 'n' dirty reverse:

Code Sample

#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

echo $1 | sed 's/[-:]//g' | while read -n 2 HEXCH; do printf "%s\x$HEXCH" 2>/dev/null; done | sed 's/\\\x$/\n/'


Will ignore - or : in input.

These are for 13 char asci-equivalent keys.
Back to top
Profile PM 
WDef Offline





Group: Members
Posts: 798
Joined: Sep. 2005
Posted: Sep. 18 2007,19:57 QUOTE

A Perl version, since I definitely need the practice:

Code Sample
#!/usr/bin/perl


#
# hex2ascikey.pl
# Takes hex wifi key as argument and prints asci
#

use strict;
use warnings;

my $hexkey = shift;
chomp $hexkey;
$hexkey =~ s/^[+]+//;  # or can't cope with a leading +
$hexkey =~ s/[:-]//g;

# Insert space every 2 chars
$hexkey =~ s/([^\s]{2})/$1 /gs;

my @A = split (/ /, $hexkey);

foreach my $c (@A){
my $dec = hex($c);
print chr($dec);
}

print "\n";



EDIT:   Perl version as is can't cope with $ as an illegal input hex char.   Gives superior built-in error messaging and tells you that it is ignoring which illegal hex chars.

The bash script runs in around the same time or faster, despite the pipes (subshells), read and 2 sed invocations - not that speed matters here.

Equivalent functionality in bash to both flag and remove illegal hex chars is like:

Code Sample

#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

echo -n $1 | sed  's/[0-9A-Fa-f]//g
t message
:message s/^.\+$/Ignoring non-hex chars &\n/'

echo -n $1 | sed 's/[^0-9A-Fa-f]//g' | while read -n 2 HEXCH; do
printf "%s\x$HEXCH" 2>/dev/null; done | sed 's/\\\x$//'

echo


which is more sed than I usually care to learn and shows why I like Perl.  No doubt this could be made better. To cope with $ as a non-hex input char, bash script argument needs to be put in single quotes.

A more readable alternative bash script might parse the input char by char instead.
Back to top
Profile PM 
13 replies since Sep. 18 2007,12:33 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (3) </ 1 [2] 3 >/
reply to topic new topic new poll
Quick Reply: Convert ascii wifi key to hex

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code