Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I change integer string to hex in php?

I'm a beginner at php so please forgive me if this seems like a noobish question. I've been reading tutorials and the php manual but I can figure this out.

How do I take a variable with an integer string data and output it as the hex character?

for example

<?php

$data = "65";

$the_e = "\x".$data;

echo $the_e;

?>

outputs the string "\x65" when I want the character 'e'. Is there a simple way to do this I've been looking for a work-around but I need to have the hexcode from a variable or I'd need a switch statement that handles each byte.

<?php

$data = $_POST[variable];

switch($data){

case "00":

echo "\x00";

break;

case "01":

echo "\x01";

break;

ect...

?>

I'd really rather not do that for obvious reasons. If anybody know how to do this or has a better work-around please let me know, thanks.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    There are two PHP functions that does conversion between integers and characters:

    <?php

    $char = "e";

    $int = 65;

    $char == chr($int); # true, as chr(65) is 'e'

    $int == ord($char); # true, as ord('e') is 65

    ?>

    So, to summarize, chr() converts and integer to a character and ord() does the opposite.

Still have questions? Get your answers by asking now.