Forum Moderators: coopster & phranque

Message Too Old, No Replies

Converting alphanumerical characters to hexidecimal

Looking for utility...

         

Key_Master

1:10 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm looking for a script or utility that will convert a string consisting of both non-alphanumerical and alphanumerical characters into hexadecimal.

E.g., "text" becomes "%74%65%78%74"

Thanks in advance.

Xoc

1:16 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For what scripting language/web server?

Key_Master

1:20 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Practical Extraction and Report Language running on a Uniplexed Information and Computing System.

Aka, Perl on Unix. ;)

Brett_Tabke

6:55 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I don't think this does exactly what you want, but it might work (don't even know if the url is right). Or are you looking to encode urls?

#!/usr/local/bin/perl
#
# @(#) $Id: bin2hex.pl,v 1.9 1999/03/02 13:17:53 jaalto Exp $
# @(#) Perl -- FILE, Convert binary file to hex dump
#
# File id
#
# .Copyright (C) 1998-1999 Jari Aalto
# .Created: 1998-05
#.Contactid:<jari.aalto@poboxes.com>
#.Keywords:Perl file conversion bin hex
#.Url:http://www.netforward.com/poboxes/?jari.aalto

I think it's here. [planet-source-code.com]

Key_Master

11:34 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually what I am looking to do is encode variables in Perl. Basically, I need a utility of some type that will spit out the hexadecimal equivalent of any letters I enter into it. I will then cut and paste the output into my Perl scripts. Right now, I have to refer to a chart and hand code each alphanumerical character with it's hexadecimal equivalent (time consuming).

Brett_Tabke

11:46 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That bin2hex will work then. It grabs any input file and converts it to data type statements.

Key_Master

11:56 am on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure Brett? It says, This program makes a binary file out of HEX input data.

Woz

11:42 pm on Jan 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a site listing a whole stack of HTML and CSS standards and such likes here [blooberry.com]. You may be able to use the information there to create your own system which is what I am goung to do.

Onya
Woz

sugarkane

12:20 am on Jan 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the basic code to do the job - it could quite easily be wrapped into a CGI or command line script:

[perl]
#!/usr/bin/perl

$input="text";

@tmp_array=unpack('C*', $input);
foreach $i (@tmp_array) {
$hex=sprintf "%lx", $i;
$output.="\%$hex";
}

print "$output\n";
[/perl]

Key_Master

1:15 am on Jan 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help guys. surgarkane, thanks for the code tidbit. Wish I got it sooner- I spent the whole day encoding the variables by hand :). But after seeing what is possible with Dec & Hx encoding in Perl I know I'll find it very using in future projects.

Key_Master

3:02 am on Jan 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PERL Utility: Convert Text to HEX


#!/usr/local/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Convert Text to HEX</title>\n";
print "</head>\n";
print "<body>\n";
print "<font color=\"#000030\" face=\"Arial\" size=\n";
print "\"3\"><b>Convert Text to HEX</b>\n";
print "<p><br></p>\n";

if ($ENV{'CONTENT_LENGTH'}) {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$input = $FORM{'message'};
@tmp_array = unpack("C*",$input);
print "<font color=\"#000030\" face=\"Arial\" size=\"2\"><b>Answer: </b>";
foreach $i (@tmp_array) {
$key = sprintf("%lx",$i);
$output = "\%$key";
print $output;
}
$input =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
}
else {
$input = "Enter text here.";
}

print "</font>\n\n";
print "<form method=\"post\" action=\"/cgi-bin/hex.pl\">\n";
print "<textarea cols=\"65\" rows=\"5\" name=\"message\">$input</textarea><br>\n";
print "<input type=\"submit\" value=\"Convert to HEX\" style=\n";
print "\"background: #000033; color: #FFFFFF; font-family: 'Arial', sans-serif; font-weight:bold; font-size: 8pt\">\n";
print "</form>\n";
print "</body>\n";
print "</html>\n";
exit;

Brett_Tabke

3:30 am on Jul 7, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Sorry KM, I missed the followups on this the first time around.

There is bintohex.pl and hextobin.pl (they do just the opposite). I can not find bintohex.pl on that site I mentioned above.

(sidebar: that side [planet-source-code] is a disgrace and embarassment to all programmers. I was just on a page that had 4700 html errors)