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

  • SEARCH
  • Home
  • 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 8193819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:36:47+00:00 2026-06-07T04:36:47+00:00

Haven’t slept in awhile, so I’m probably missing something simple. Basically, I am taking

  • 0

Haven’t slept in awhile, so I’m probably missing something simple. Basically, I am taking a number and converting it to three characters. Max number of possibilities is 256*256*256 (16777216). I convert it with:

public function s_encode($num) {
  $num = chr($num / 65536).chr($num / 256).chr($num % 256);      
  return bin2hex($num);  
} 

And convert it back with:

public function s_decode($hex) {    
  $a = pack("H*", $hex);
  $b = ord(substr($a, 1, 1));
  $c = ord(substr($a, 2, 1));
  $d = ord(substr($a, 0, 1));   
  return (($d * 65536) + ($b * 256)) + $c;
}

What’s strange, is this actually works. It does what I want it to, but how could it? In the first code, where I convert it to three characters, the second part of the conversion is:

chr($num / 256)

If the number is greater than 65536, this should cause an error, but it doesn’t. If I were to use unpack instead of bin2hex, it will cause an error. bin2hex won’t. Why and how is bin2hex so magical?

  • 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-06-07T04:36:49+00:00Added an answer on June 7, 2026 at 4:36 am

    chr() only looks at the lowest 8 bits of its input:

    echo "'".chr(320)."'";
    

    yields…

    '@'
    

    as does…

    echo "'".chr(64)."'";
    

    http://ideone.com/65Itz

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I haven't seen easy examples about rotaion around a specified point. I tried something
I haven't really been able to find any good simple tutorials an animating a
Haven't found in docs. Does java ResultSet supports query arguments,like jdbcTemplate? For example, something
Haven't seen anything about it here but it seems to solve one of the
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
I haven't been able to get this working and all of the sample code
I haven't actually built an app yet, but I'm confused by documentation on bind-attr.

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.