Home > Archive > PHP General > August 2004 > LRC (longitudinal redundancy check) and Even Parity
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
LRC (longitudinal redundancy check) and Even Parity
|
|
| Dominic Schanen 2004-08-12, 4:03 pm |
| Hello,
I'm working on some code to authorize and settle directly with Vital.
Some of the inspiration and code samples have come from the Perl module
Business::OnlinePayment::VirtualNet.
Things are working good approximately 99% of the time. However, my
communication is getting Nak'd on settlement before getting to Vital.
When I reassign my batches, it comes down to one transaction that is
causing the problem. I believe the problem is either in my LRC
(longitudinal redundancy check) calculation or my conversion of the
string to even parity.
I'm hoping there is someone out there that knows more about LRC and even
parity than I do that can look over the below functions to see if they
are calculating the LRC and even parity correctly. In the last 3 that
have gotten Nak'd, it appears the commonality between the transactions
is that my LRC value is a 'nul' character (0000000 binary) on the detail
record. So I'm either thinking that I'm not converting the nul character
to even parity correctly or my LRC is slightly off and it shouldn't be a
nul character.
If there may be functions in PHP that I'm overlooking that could do this
easier for me, please let me know as well. Thanks to anyone you might
lend some advice.
Here are my functions:
/************* LRC ************************/
/*** taken from Perl module String::LRC ***/
function lrc($buffer)
{
$len = 0;
if (isset($buffer) && !empty($buffer)){
$len = strlen($buffer);
}
$check = substr($buffer, 0, 1);
for ($i = 1; $i < $len ; $i++) {
$check ^= substr($buffer, $i, 1);
}
return $check;
}
/************* Set Even Parity *******************/
function setEvenParity($str)
{
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $key => $val){
$chars[$key] = evenParityChar($val);
}
return implode("", $chars);
}
function evenParityChar($char)
{
$bin = sprintf("%07b",ord($char));
$check = substr($bin, 0, 1);
for ($i = 1; $i < strlen($bin); $i++){
$check ^= substr($bin, $i, 1);
}
$dec = pack('C', bindec($check . $bin));
return $dec;
}
Thanks,
Dominic
| |
| Dominic Schanen 2004-08-12, 8:57 pm |
| I was calculating the LRC before setting even parity. I'm hoping that
was my problem. If so, this is the change that I made, in case anyone
else can find use in these functions:
/************* LRC ************************/
/*** taken from Perl module String::LRC ***/
function lrc($buffer)
{
$len = 0;
$buffer = setEvenParity($buffer);
if (isset($buffer) && !empty($buffer)){
$len = strlen($buffer);
}
$check = substr($buffer, 0, 1);
for ($i = 1; $i < $len ; $i++) {
$check ^= substr($buffer, $i, 1);
}
return $check;
}
We'll see how this goes tomorrow.
Dominic
|
|
|
|
|