Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to hex

Status
Not open for further replies.

tomchesley

Technical User
Apr 16, 2006
17
0
0
US
I am trying to convert a numerical string to its hex equivelent.
Say for instance
if $u = "1"
i want $h = "01"
if $u = "10"
$h = "0a"
and so on

The $u will never go above 255
There ha to be a better way
at moment i have

Code:
if ($u < 10) {
    $h = "0".$h;
}
if ($u == 10) {
    $h = "0a";
}
.
.
if ($u == 16) {
    $h = 10;
}
 
Thank you Thank you that worked

I tried earlier
Code:
$hexval =~ s/(.|\n/sprintf(%02lx, ord $1)/eg
but didnt work
Your code did the trick for me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top