Ras has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Convert hex to string
by Juerd (Abbot) on Dec 29, 2001 at 04:32 UTC
    What's "a hex"?

    If you mean a string of hex characters (like "252525" if you want "%%%"), pack "H*", $string is the answer.
    If you want just a single character (like "%" out of "25"), hex($string).

    Update - BTW, did I just answer your homework assignment?

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      no. I'm tring to help some one out on there project and i though that this would work:

      $hex = 9120;
      $string = sprintf("%s", hex($hex));
      print "$string \n";

      but it doesn't. I still need help.
        That doesn't make sense. Your source is 9120, but what do you want?
        Are you sure this isn't homework?

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Well i'm trying to convert that hex number "9210" into a string. And believe me this is not homework. Some one in my office wants to know if this can be done.
        Someone in your office wants to know if it can be done?

        That's got to be the most hilarious thing I've heard in a long time. And do they have two tufts of pointy hair as well?

        Seriously, though .. you need to understand number bases to understand what's going on here. Just telling you that the answer is 63,360 (which it isn't) isn't going to help you.

        You need to decompose the number into tens, hundreds and thousands, then remember that in hexadecimal, ten is actually 16, a hundred is actually 256 and a thousand is actually 4,096. So then it's 9 x 4,096 plus .. you get the idea.

        Alternatively, you could convert 0x9210 to binary, giving 1001 0010 0001 0000, then just multiplying by the appropriate powers of two. Actually, that looks pretty easy .. 16,384 + 4096 .. well, you get the idea.

        Alternatively, in C you could sscanf the number in using "%x", then print it out using "%d" and get the machine do to the conversion.

        The Windows calculator application also converts between hex and decimal.

        So, anyway, good luck.

        --t. alex

        "Excellent. Release the hounds." -- Monty Burns.

        After reading all of these posts, I still have no idea what your requirements are. What should this string be? Do you have an example of the input and output?

        You may just be looking to use the %x format for sprintf, which takes a number and constructs a hexadecimal representation of it. But since you say 9210 is a hex number to begin with, I haven't a clue what this string should contain. A decimal representation of the number? Use hex. Otherwise be more specific.