I want to convert C code block to equivalent PHP code - php

if (crc & 0x8000){
crc = crc << 1 ^ 0x1021;
}
Above code block is in C language.
what would be the equivalent php code.

You can use bitwise operators and inline hex numbers in PHP just fine. The equivalent should just be:
if ($crc & 0x8000){
$crc = $crc << 1 ^ 0x1021;
}

Related

How can I rewrite this JavaScript to PHP?

Following are some individual JavaScript statements from a block of java-script code. I want to convert this JavaScript code in PHP so that I can execute it server-side. In the following snippet, there are some operators used, such as >>, >>>, ~, ^, <<, <<<, etc.
How can I translate these JavaScript functions to PHP?
function core_enc(K, F) {
K[F >> 5] |= 128 << ((F) %32);
K[(((F + 64) >>> 9) << 4) + 14] = F;
}
function enc_ff(C, B, G, F, A, E, D) {
return enc_cmn((B & G) | ((~B) & F), C, B, A, E, D);
}
function enc_hh(C, B, G, F, A, E, D) {
return enc_cmn(B ^ G ^ F, C, B, A, E, D);
}
function safe_add(A, D) {
var C = (A & 65535) + (D & 65535);
var B = (A >> 16) + (D >> 16) + (C >> 16);
return (B << 16) | (C & 65535);
}
function bit_rol(A, B) {
return (A << B) | (A >>> (32 - B));
}
That would be the exact same operators. See http://codepad.org/b5uZPCu4 for a demo.
The shift operators >> and << can be directly translated, as well as other basic bitwise operations such as | (or), & (and), ^ (xor), ~ (not), etcetera. PHP arrays also work in the same fashion, and the functions have about the same syntax. Your JavaScript code should work perfectly in PHP with minimal changes (e.g. variableName becomes $variableName, you remove var, and >>> doesn't work in PHP.)
Edit: If this is actually code for MD5 or something, PHP provides the convenient function md5 to do that for you.

Swapping bytes of text in PHP

I basically need to port this piece of code to php
for (i = 0; i < 128/4; i++)
data32[i] = bswap_32(data32[i]);
But, there is no bswap function in php.
Would someone be kind enough to provide me with something that could solve the problem?
This should do it (untested):
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (($j & 0xff000000) >> 24);
}
Or, if there is a sign extension problem, this should resolve it:
function bswap_32($j)
{
return (($j & 255) << 24) | (($j & 0xff00) << 8) |
(($j & 0xff0000) >> 8) | (255 & (($j & 0xff000000) >> 24));
}
It sounds like bswap_32 is swapping endianness of your 32-bit quantities.
I could just give you some code, but I'd prefer not to do people's work for them, so I'll explain the principle instead:
You can achieve that with bit-shifts and masks (so for instance, you need to mask out the 8 lowest bits, and shift them into the highest 8 bit positions of the result).
Shifting can be done with the << and >> operators. Masking can be done with the & operator. See the PHP manual page on operators for more details.

PHP Bit shifting in code?

Can someone help me understand this ancient code?
$a = 00040000000002;
$n = sscanf($a,"%02x%02x%02x%02x%02x%02x%02x",$r[7],$r[6],$r[5],$r[4],$r[3],$r[2],$r[1]);
$ptemp = $r[1] + (($r[2] & 0x1F) << 8);
$l[$i] = (($r[2] & 0xE0) >> 5) + ($r[3] << 3);
$m[$i] = $r[4] + (($r[5] & 0x03) << 8);
$h[$i] = (($r[5] & 0xFC) >> 2) + (($r[6] & 0x03) << 6);
$dist[$i] = (($r[6] & 0xFC) >> 2) + (($r[7] & 0x0F) << 6);
$fruittoday[$i] = ($r[7] & 0xF0) >> 4;
I understand the sscanf, but I'm not sure what is going on with the & 0x1f << 8, etc.
Any ideas?
They're using Bitwise Operators to check if specific bits are turned on.
In the example you've provided, they're confirming the first 5 bits are 1's then shifting all bits to the left 8 places. (Although it seems like in this case they're starting with 0 (00000000), checking if 0x1F are on (00011111) which would still be 0, then shifting them left 8 places (put in to the 2^8th position)
For reference, I would check out php's manual on bitwise operators. They list some example of what's going on here. If you're really curious, just echo the variables as they are manipulated (or break it out in to separate operations) and then examine them).

bytes convert to float (php)

How can I convert from bytes to float in php? Like in Java
int i = (byte3 & 0xff) << 24 | (byte2 & 0xff) << 16 | (byte1 & 0xff) << 8 | byte0 & 0xff;
Float.intBitsToFloat(i);
There may be a more direct way, but here you go:
<?php
var_dump(unpack('f', pack('i', 1059760811)));
?>
This is, of course, machine dependent, but I don't know of any machine running PHP that doesn't use IEEE 754 floats.
I don't think php has bytes, does it?
When you assign a number to a variable you'll get an variable with a number type
$a = 10; // integer
$f = 1.0; // double
$b = $a + $f; // $b is double
If I'm understanding you correctly, you want to take a raw 32- or 64-bit "integer" value, and force that set of bits to treated as a floating point number instead?
Try the 'pack' and 'unpack' functions

PHP Bytes 2 DWord

I have an array:
$arr[0] = 95
$arr[1] = 8
$arr[2] = 0
$arr[3] = 0
That are bytes. I need a DWORD.
I tried:
$dword = $arr[0]+$arr[1]*265+$arr[2]*265*265+$arr[3]*265*265*265;
Is that right or am I doing it wrong?
Try:
$dword = (($arr[3] & 0xFF) << 24) | (($arr[2] & 0xFF) << 16) | (($arr[1] & 0xFF) << 8) | ($arr[0] & 0xFF);
It can also be done your way with some corrections:
$dword = $arr[0] + $arr[1]*0x100 + $arr[2]*0x10000 + $arr[3]*0x1000000;
Or using pack/unpack:
$dword = array_shift(unpack("L", pack("CCCC", $arr[0], $arr[1], $arr[2], $arr[3])));
Or try<?php
$arr = array(95,8,0,0);
$bindata = join('', array_map('chr', $arr));
var_dump(unpack('L', $bindata));both (Emil H's and my code) give you 2143 as the result.
Or at the very least use 256 rather than 265.
Your code should work correctly, but you should multiply with 256, not 265. (in 8 bits, there are 2^8 = 256 unique values). It works, because multiplying with 256 is the same as shifting the bits 8 places to the left.
Perhaps you should consider using the bitwise operators instead, to better convey the intent. See http://theopensourcery.com/phplogic.htm

Categories