0

I'm getting into compression algorithms and my native language is PHP, and I can understand that PHP is a language that you wouldn't normally make a big algorithm in, but I was wondering if it was possible. (of course it's possible - more so efficient and powerful)

The first type of algorithm I'd attempt to make is a simple adaptive algorithm, by taking in the most used bytes (chars) and converting them into binary types, (ex: a = 0001, b = 0010, c = 0011) - tho there is no real way to do that in PHP that I am familiar with, before this I was using simple ASCII conversions like a = chr(33), b = chr(34) that would get the least valued ASCII values use them as the smallest -> largest operators for the compression definitions.

So what I am asking is that if there's a way to assign binary values to a variable instead of it being represented as ASCII, if I go:

$string_after_compression = 000100100011;
#split it by 4 bits per   = 0001 | 0010 | 0011

That would be interpreted as an int - therefore making an over-large int therefore most likely running out of available ram with a simple sentence, then if I try to store the value in a string instead, that removes the point of compressing as it's just making a string like:

$string_after_compression = "000100100011";
#split it by 4 bits per   = "0001" . "0010" . "0011";

-----

This question is a bit confusing but the aim is: Is there a way to assign a PHP variable using a PHP intigers

Example solution:

$binary_var = (binary) 0001;
3
  • Have you looked at : php.net/manual/en/function.decbin.php, as stated in the documentation, it "Returns a string containing a binary representation of the given number argument" which seems to be what your aiming to do in your exemple. Commented Oct 25, 2016 at 7:24
  • Yeah I've taken a look at it before and I did think it was interesting, might be worth implementing but I'm guessing it returns an int from the decbin / bindec functions. :-) Commented Oct 25, 2016 at 7:32
  • pack Commented Oct 25, 2016 at 7:35

1 Answer 1

3

To work with binary in PHP, your data type of choice is a string. Because strings are mere byte arrays:

$bytes = '';

There's no simple base 2 notation for binary in PHP (0100101), usually the next best option to work with binary is hex notation:

$bytes = "\x42";  // 0100 0010

You'll need to convert between base 2 and base 16 notation back and forth in your head, but once you get used to that it's typically easier to follow and work with than long strings of 1s and 0s.

To test or manipulate binary data you'll want to get used to the binary operators:

if (($bytes[3] & "\x02") === "\x02") {
    // the second bit of the forth byte in the sequence is set (0000 0010)
}

$bytes[6] |= "\x02";  // setting the second bit of the seventh byte
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Just reading up on Hexadecimal and Base-2 notation, gladly appreciated friend. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.