Jump to content

Archived

This topic is now archived and is closed to further replies.

HardCoreMore

How to load file as binary data and operate on it with bitwise operators?

Recommended Posts

Hi all,

 

Is it possible in php to read data from file as binary and operate on it with bitwise operators and save result as binary.

 

Thanks

Share this post


Link to post
Share on other sites

If you already have the contents of the file as binary string you can use binarysafe fread() and fopen()

If you need to convert your data to binary you could use PHPs pack() function. I took this example from PHP.net:

<?php
    public function unpack_str($str, $len) {
        $tmp_arr = unpack("c".$len."chars", $str);
        $out_str = "";
        foreach($tmp_arr as $v) {
            if($v>0) {
                $out_str .= chr($v);
            }
        }
       
        return $out_str;
    }
   
    public function pack_str($str, $len) {       
        $out_str = "";
        for($i=0; $i<$len; $i++) {
            $out_str .= pack("c", ord(substr($str, $i, 1)));
        }
        return $out_str;
    }
?>

 

Using those functions for your retrieved data will convert it to binary string. After that you should be able to operate with bitwise operators.

Share this post


Link to post
Share on other sites

Hey johnny86,

 

Thanks for the reply. But i can't figure out what phps unpack function is doing. Can you elaborate that a little please.

 

Share this post


Link to post
Share on other sites

Okay, that was obviously just not what you were looking for =)

 

What about base_conver? http://fi.php.net/base_convert

 

You could then convert your strings to raw binary 10101010010101 with:

<?php

$string = "someString";

function toBin($string)
{
    $out = "";
    for($i = 0; $i < strlen($string); $i++)
    {
        $out .= base_convert(ord($string[$i]), 10, 2);
    }
    return $out;
}
?> 

Share this post


Link to post
Share on other sites

Thanks johny. That was exactly that i was looking for. Although i read on php.net that base_convert has some problems with big numbers but that is not a problem here since characters are 8 bit. Thanks again :)

Share this post


Link to post
Share on other sites

I forgot something. The reason i asked this is that i didn't quite understand how fopen and opening files generally work.

 

So it is necessary to loop through string to convert it to binary like this:

 

for($i = 0; $i < strlen($string); $i++)
    {
        $out .= base_convert(ord($string[$i]), 10, 2);
    }

 

can fopen open it in a stream of zeros and ones.

 

And one more thing. Is it possible to do calculations on big binary at once. For example when i do

 

1010 & 1111

 

that is equal 1010 obviously. But how can i do such operations on big raw values. For example php int are 32 bit and can hold signed values for about 2 billion as specified in documentation. If i give it big raw binary number that will not work. On the other hand

if i use string to make binary how can i than use bitwise operators. I am not clear about how to achieve this.

 

if i have raw binary number that is( i will represent it in decimal to spare the room )

 

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

 

so i can't assign this number to php var because its to big. if i convert it to STRING OF BINARY ONES AND ZEROS how can i then apply bitwise operations on string?

 

 

Hope you understand what i am trying to do here if that is possible and make sense. Thanks

 

If you can't operate on big values at once than what is workaround

Share this post


Link to post
Share on other sites

PHPs bitwise operators work like this on strings:

PHP will output ASCII characters when string is compared.

 

For example: "A" & "A" = "A" and "A" & "B" = "@"

 

"A" & "B" = "@" can be turned into this:

// ASCII NUMBERS (integers)
$i1 = ord("A");
$i2 = ord("B");

$c = $i1 & $i2; // This comparsion is made according to ASCII numbers in binary level and will return 64

// Finally PHP outputs ascii character:
echo chr($c); // @ 

 

So PHP will go to binarylevel in comparsion. PHP will compare every characters ASCII number seperately in binary level. PHP then takes the result and spits out ASCII character that is equal to the result.

 

So "AAA" & "AAA" = "AAA"

And "AAB" & "AAB" = "AA@"

 

Does that help you at all?

Share this post


Link to post
Share on other sites

Edit: I wrote this at the same time as johnny's reply.

 

PHP's bitwise operators work on integers, so you can use them for no more than 32 bits at a time (or 64 bits if you have 64 bit php).  You might need to make your own bit manipulation functions.

 

If I was doing it I would probably make a function which takes binary data encoded as a string, and does bitwise operations on 1 character at a time.  Something like this:

 

function string_or($a, $b) {
  $a_len = strlen($a);
  $b_len = strlen($b);
  $r = '';
  for ($i = 0; $i < min($a_len, $b_len); $i++) {
    $r .= chr(ord($a{$i}) | ord($b{$i}));
  }
  if ($a_len > $i) $r .= substr($a, $i);
  if ($b_len > $i) $r .= substr($b, $i);

  return $r;
}

 

Note how chr() and ord() let you convert a character into an integer and back.  $str{$i} gives you the i'th character from a string.

Share this post


Link to post
Share on other sites

U do know that just a general question will get u a variety of answers.

can fopen open it in a stream of zeros and ones.

No, it will work on a stream of bytes (a byte has 8 bits)

Hope you understand what i am trying to do here if that is possible and make sense. Thanks

Without a detail explanation, or code snippets your questions are vague and generalized, so u will get answers based on what ppl assume you want.

 

If you can't operate on big values at once than what is workaround

that all really depends what yer operations are on your data

 

if yer doing a simple bitflags system, than u dont need a whole lot just Get/Set/Clear/Toggle Bit functions.

 

if your were say encrypting than u need to convert the stream into char/ints.

 

But all these really needs a clear explanation of what yer trying to accomplish

 

Share this post


Link to post
Share on other sites

 

PHP's bitwise operators work on integers, so you can use them for no more than 32 bits at a time (or 64 bits if you have 64 bit php).  You might need to make your own bit manipulation functions.

 

If I was doing it I would probably make a function which takes binary data encoded as a string, and does bitwise operations on 1 character at a time.  Something like this:

 

function string_or($a, $b) {
  $a_len = strlen($a);
  $b_len = strlen($b);
  $r = '';
  for ($i = 0; $i < min($a_len, $b_len); $i++) {
    $r .= chr(ord($a{$i}) | ord($b{$i}));
  }
  if ($a_len > $i) $r .= substr($a, $i);
  if ($b_len > $i) $r .= substr($b, $i);

  return $r;
}

 

Note how chr() and ord() let you convert a character into an integer and back.  $str{$i} gives you the i'th character from a string.

 

Thanks johny. Now i understand both what i actually wanted and how to accomplish it.

 

PHPs bitwise operators work like this on strings:

PHP will output ASCII characters when string is compared.

 

For example: "A" & "A" = "A" and "A" & "B" = "@"

 

"A" & "B" = "@" can be turned into this:

// ASCII NUMBERS (integers)
$i1 = ord("A");
$i2 = ord("B");

$c = $i1 & $i2; // This comparsion is made according to ASCII numbers in binary level and will return 64

// Finally PHP outputs ascii character:
echo chr($c); // @ 

 

So PHP will go to binarylevel in comparsion. PHP will compare every characters ASCII number seperately in binary level. PHP then takes the result and spits out ASCII character that is equal to the result.

 

So "AAA" & "AAA" = "AAA"

And "AAB" & "AAB" = "AA@"

 

Does that help you at all?

 

 

Yes that helps. Thank you.

 

 

U do know that just a general question will get u a variety of answers.

 

Yes i know that is why i asked it that way.

 

can fopen open it in a stream of zeros and ones.

No, it will work on a stream of bytes (a byte has 8 bits)

 

Thank you. This was helpful. Now it makes sense.

 

Hope you understand what i am trying to do here if that is possible and make sense. Thanks

Without a detail explanation, or code snippets your questions are vague and generalized, so u will get answers based on what ppl assume you want.

 

Yes i know. I asked that way because i myself didn't know what i what exactly nor what to ask exactly because i never done such things before and that is why i asked such question so that i can see what people saying so i can learn what i don't know and to be able to ask right question. You got to have some knowledge in the first place to be able to ask right question. So i needed that. Sorry if i bugged anyone.

 

If you can't operate on big values at once than what is workaround

that all really depends what yer operations are on your data

 

if yer doing a simple bitflags system, than u dont need a whole lot just Get/Set/Clear/Toggle Bit functions.

 

if your were say encrypting than u need to convert the stream into char/ints.

 

But all these really needs a clear explanation of what yer trying to accomplish

 

 

 

This was also helpful. Thank you.

 

 

 

Thanks all. Now things are a lot clearer.

 

 

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.