Encoding-Convertor.php - Encoding Conversion Test

This section provides a tutorial example to restore corrupted Chinese text with different suggested solutions.

When are you trying to recover corrupted Chinese text, you may want to convert the hex string of an input to a given encoding.

Here is a PHP script I wrote to do this:

#- Encoding-Convertor.php
#- Copyright (c) 2005 HerongYang.com. All Rights Reserved.

  $input = "E9B8A1";
  $encodeIn = "UTF-8";
  $encodeOut = "UTF-8";
  if (isset($argv[1])) $input = $argv[1];
  if (isset($argv[2])) $encodeIn = $argv[2];
  if (isset($argv[3])) $encodeOut = $argv[3];

  $output = iconv($encodeIn, $encodeOut, hex2bin($input));

  print("input = ($input)\n");
  print("hex2bin(input) = (".strtoupper(hex2bin($input)).")\n");
  print("output = ($output)\n");
  print("bin2hex(output) = (".bin2hex($output).")\n");

Let's try an example:

herong$ php Encoding-Convertor.php E9BCAC latin1 utf8 

input = (E9BCAC)
hex2bin(input) = ()
output = (鼬)
bin2hex(output) = (C3A9C2BCC2AC)

The output tells me the following:

Table of Contents

 About This Book

 PHP Installation on Windows Systems

 Integrating PHP with Apache Web Server

 charset="*" - Encodings on Chinese Web Pages

 Chinese Characters in PHP String Literals

 Multibyte String Functions in UTF-8 Encoding

 Input Text Data from Web Forms

 Input Chinese Text Data from Web Forms

 MySQL - Installation on Windows

 MySQL - Connecting PHP to Database

 MySQL - Character Set and Encoding

 MySQL - Sending Non-ASCII Text to MySQL

 Retrieving Chinese Text from Database to Web Pages

 Input Chinese Text Data to MySQL Database

Chinese Text Encoding Conversion and Corruptions

 Detect System Default Encoding

 Root Cause of Corrupted Chinese Text

 Corrupted Chinese File Name with Un-ZIP

 Generate 8-Bit Encoding Tables

 Restore Corrupted Chinese Text

Encoding-Convertor.php - Encoding Conversion Test

 Archived Tutorials


 Full Version in PDF/EPUB