Log in

Registration

PHP: Convert Hex Strings to ASCII Strings

Posted: March 28, 2010 / in: Scripts / No comments

source-php-iconThis recipe will show you how to perform a simple conversion from hexadecimal strings to ASCII character strings in PHP.

Convert Hexadecimal Strings

The following function illustrates how to convert a simple hexadecimal repesentation of strings to ASCII character strings. When utilizing this algorithm please be sure to remove blanks and other whitespaces before calling the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

  /* convert a string of hex values to an ascii string */
  function hex2str($hex) {
    for($i=0;$i<strlen($hex);$i+=2)
    $str .= chr(hexdec(substr($hex,$i,2)));

    return $str;
  }

  // input strings are not case-snsitive
  echo hex2str("48656c6c6f2c20576f726c64210a");
  echo hex2str("48656C6C6F2C20576F726C64210A");

/* Output:
  Hello, World!
  Hello, World!
*/


?>

 

 

 

Incoming search terms:

Tags:

© Copyrights and Licenses, 2014 - Linux-Support.com The Professional Linux and OSS Services Portal