[PHP] crc32() and hash('crc32') Differ

2007-03-18 Thread Morgan Doocy

Hi,

I'm curious why I'm getting two different digests of the same message 
using crc32() and hash('crc32'). I've tried both hash('crc32') and 
hash('crc32b'), and neither match my crc32(). Some example code:


?php
$message = foo;

$crc32 = crc32($message);
$hash_crc32 = hash('crc32', $message);
$hash_crc32b = hash('crc32b', $message);

$data = array (
array (
sprintf('%+011d', $crc32), // signed integer
sprintf('%010u', $crc32),  // unsigned integer
dechex($crc32),// hexadecimal
sprintf('%032b', $crc32)   // binary
),
array (
sprintf('%+011d', hexdec($hash_crc32)),
sprintf('%010u', hexdec($hash_crc32)),
$hash_crc32,
sprintf('%032b', hexdec($hash_crc32))
),
array (
sprintf('%+011d', hexdec($hash_crc32b)),
sprintf('%010u', hexdec($hash_crc32b)),
$hash_crc32b,
sprintf('%032b', hexdec($hash_crc32b))
)
);

	echo join(\n, array_map(create_function('$vals', 'return join( = 
, $vals);'), array_values($data)));

?

Output:

-1938594527 = 2356372769 = 8c736521 = 1000110001110011011001010011
-1513816503 = 2781150793 = a5c4fe49 = 1010010111000100111001001001
+0560296844 = 0560296844 = 2165738c = 0011011001010111001110001100

Not only do none of these match, but when I try with a message that I 
intend to validate and am given a CRC32 to check against, the given 
CRC32 hash does not match any of these three. Granted, the message I'm 
trying to validate could in fact be invalid, but regardless there's 
still the discrepancy between crc32() and hash('crc32'). Is this 
expected behavior?


Thanks,

Morgan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Referencing Containing (Non-Parent) Object?

2005-10-29 Thread Morgan Doocy
I'm trying to figure out if PHP has the facility to reference containing, 
non-parent objects. I have three classes, embedded hierarchically, but which 
are NOT extended classes of their containing objects. I'd like to be able to 
reference variables in the higher-level objects from the lower-level objects.

To illustrate:

?php

class Country {
var $name;
var $states;

function Country($name = '', $states = array()) {
$this-name = $name;
$this-states = $states;
}
}

class State {
var $name;
var $cities;

function State($name = '', $cities = array()) {
$this-name = $name;
$this-cities = $cities;
}
}

class City {
var $name;

fuction City($name = '') {
$this-name = '';
}

function name_with_state() {
return $this-name,  . /* parent node reference */-name;
}
}

$countries = array (
'USA' = new Country('United States',
array (
'WA' = new State('Washington',
array (
'SEA' = new City('Seattle'),
'GEG' = new City('Spokane')
)
),
'CA' = new State('California',
array (
'LAX' = new City('Los Angeles'),
'SFO' = new City('San Francisco')
)
)
)
)
);

echo $countries['USA']-states['WA']-cities['SEA']-name_with_state();
// Output: 'Seattle, Washington'

?

Obviously, just extending Country doesn't make sense: a State isn't a type of 
Country, and a City isn't a type of State -- but States are part of Countries, 
and Cities are part of States. Plus, if City was just an extended State (which 
in turn is an extended Country), it would contain both a $states and a $cities 
variable. Which doesn't make sense.

What, if anything, do I replace /* parent node reference */ with in 
City::name_with_state()?

Thanks,

Morgan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php