The simple answer is to create the string with sprintf "%x". There's more information about controlling the hexadecimal formating in the Categorized Q&A How do I convert from decimal to hexidecimal?.

(updated to correct my hexi->hexa but leave the Q link wrong)

That's the end of the practical answer. Flight of fancy follows...

Unfortunately, once you create the string '100' from 0x100 (=256), you need to be sure you are only using it as a string, not as a number, so that nothing interprets it as the number 10*10. But never fear, perl has the tools to let you create a variable that has distinct string and numeric values:
$ perl -wlne'eval;print$@if$@ # interactive perl' use Scalar::Util 'dualvar'; $x = dualvar(256, sprintf "%x", 256); print $x; 100 # correct hexadecimal string print 0+$x; 256 # correct numeric value
But there is a problem with that. If some well-meaning code tries to change our value, we lose either the string or numeric value:
$x .= "foo"; print 0+$x; Argument "100foo" isn't numeric in addition (+) at (eval 9) line 1, <> + line 8. 100 print $x; 100foo $x = dualvar(256, '100'); $x += 3; print 0+$x; 259 print $x 259
Kind of makes you want some way to turn on a readonly flag for a scalar you've set up this way.
require 5.008; Internals::SvREADONLY($x, 1)

In reply to Re: passing hex argument as string by ysth
in thread passing hex argument as string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.