Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:24:20+00:00 2026-05-13T08:24:20+00:00

I asked a question previously to get a UCS-2/HexEncoded string from UTF-8, and I

  • 0

I asked a question previously to get a UCS-2/HexEncoded string from UTF-8, and I got some help from some guys at the following link.

UCS2/HexEncoded characters

But now I need to get the correct UTF-8 from a UCS-2/HexEncoded string in PHP.

For the following strings:

00480065006C006C006F will return ‘Hello’

06450631062d0628064b06270020063906270644064500200021 will return (!مرحبا عالم) in arabic

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:24:20+00:00Added an answer on May 13, 2026 at 8:24 am

    You can recompose a Hex-representation by converting the hexadecimal chars with hexdec(), repacking the component chars, and then using mb_convert_encoding() to convert from UCS-2 into UTF-8. As I mentioned in my answer to your other question, you’ll still need to be careful with the output encoding, although here you’ve specifically requested UTF-8, so we’ll use that for the upcoming sample.

    Here’s a sample that does the work of converting UCS-2 in Hex to UTF-8 in native string form. As PHP currently doesn’t ship with a hex2bin() function, which would make things very easy, we’ll use the one posted at the reference link at the end. I’ve renamed it to local_hex2bin() just in case it conflicts with a future version of PHP or with a definition in some other 3rd party code that you include in your project.

    <?php
    function local_hex2bin($h)
    {
    if (!is_string($h)) return null;
    $r='';
    for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
    return $r;
    };
    
    header('Content-Type: text/html; charset=UTF-8');
    mb_http_output('UTF-8');
    echo '<html><head>';
    echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    echo '</head><body>';
    echo 'output encoding: '.mb_http_output().'<br />';
    $querystring = $_SERVER['QUERY_STRING'];
    // NOTE: we could substitute one of the following:
    // $querystring = '06450631062d0628064b06270020063906270644064500200021';
    // $querystring = '00480065006C006C006F';
    $ucs2string = local_hex2bin($querystring);
    // NOTE: The source encoding could also be UTF-16 here.
    // TODO: Should check byte-order-mark, if available, in case
    //       16-bit-aligned bytes are reversed.
    $utf8string = mb_convert_encoding($ucs2string, 'UTF-8', 'UCS-2');
    echo 'query string: '.$querystring.'<br />';
    echo 'converted string: '.$utf8string.'<br />';
    echo '</body>';
    ?>
    

    Locally, I called this sample page UCS2HexToUTF8.php, and then used a querystring to set the output.

    UCS2HexToUTF8.php?06450631062d0628064b06270020063906270644064500200021
    --
    encoding: UTF-8
    query string: 06450631062d0628064b06270020063906270644064500200021
    converted string: مرحبًا عالم !
    
    UCS2HexToUTF8.php?00480065006C006C006F
    --
    output encoding: UTF-8
    query string: 00480065006C006C006F
    converted string: Hello
    

    Here’s the link to the original source of the hex2bin() function.
    PHP: bin2hex(), comment #86123 @ php.net

    Also, as noted in my comments before the call to mb_convert_encoding(), you’ll probably want to try and detect which endian ordering is in use by the source, especially if your application has parts where one or more CPUs on one server differ from the rest by orientation.

    Here’s a link that can help you identify the byte-order marks (BOM).
    Byte order mark @ Wikipedia

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 312k
  • Answers 312k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Failing to find another alternative you can try the mage… May 13, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer There is an example of that in the discussion forums… May 13, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer You can't use include statement inside class, only inside methods.… May 13, 2026 at 10:34 pm

Related Questions

I previously asked a question regarding MX records (and appreciate the thoughtful answers I
I've previously asked a question about an issue I have been experiencing with CSharpOptParse
Previously I asked which DHT implementations are compatible with Python 3.x - StackOverflow's answer
Refering to a previously asked question , I would like to know how to
This is a followup question to one I previously asked: start-program-if-not-already-running-in-java I didn't get

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.