2012-12-27 78 views
0

我試圖將PHP轉換爲Perl時出現問題。這些代碼:將PHP轉換爲Perl校驗和

<form action="" method="post"> 
Hex: <input type="text" name="crc"> e.g : 08 13 4B 04 03 00 01 00 11 
<br> 
<br> 
<input type="submit" name="submit" value="submit"></form> 




<?php 

功能PHP

function crc($data) { 
    $content = explode(' ',$data) ; 
    $len = count($content) ; 
    $n = 0 ; 

    $crc = 0xFFFF; 
    while ($len > 0) 
    { 
     $crc ^= hexdec($content[$n]) ; 
     for ($i=0; $i<8; $i++) { 
     if ($crc & 1) $crc = ($crc >> 1)^0x8408; 
     else $crc >>= 1; 
     } 
     $n++ ; 
     $len-- ; 
    } 

    return(~$crc); 
} 

如果不爲空

if (!empty($_POST["submit"])) 
{ 
    echo "Input = ".$_POST["crc"].'<br>'; 
    $crc = crc($_POST["crc"]) ; 

結果

echo "<br>Result: <br>"; 
    echo "Dec = ".$crc.'<br>' ; 
    echo 'Dec - hex = '.dechex($crc).'<br>' ; 
    echo 'Checksum = '.str_replace('ffff','',dechex($crc)).'<br>' ; 

} 
?> 

這是關於校驗和。它在PHP上運行良好,但不在Perl中運行。 請給我解決方案。 謝謝。

+2

那麼Perl腳本在哪裏? – k102

+0

Perl≠PHP。告訴我們你試過了什麼 – Disa

+0

以及PHP代碼爲示例輸入計算什麼 – ysth

回答

1

正如我在評論中已經提到的,在我看來,你試圖計算CRC checksums。你的代碼看起來不對,我不明白你爲什麼不使用PHP的crc32函數。

由於您沒有提供您的方法,我們可以合作,我在Perl中編寫了一個小型的Mojolicious網絡應用程序。請注意,我使用了經過良好測試的Digest::CRC模塊,而不是半滑輪重新發明的DIY實現。

#!/usr/bin/env perl 
use Mojolicious::Lite; 
use Digest::CRC; 

# CRC helper 
helper crc => sub { 
    my ($self, $data) = @_; 
    my $ctx = Digest::CRC->new(type => 'crc32', poly => 0x8408); 
    $ctx->add($data); 
    return $ctx->digest; 
}; 

# just display the form 
get '/' => 'form'; 

# calculate the CRC 
post '/crc' => sub { 
    my $self = shift; 

    # build data from hex string 
    (my $input = $self->param('input_hex')) =~ s/\s+//g; # ignore whitespace 
    my $bytes = pack 'H*', $input; 

    # populate data for our template 
    $self->stash(
     input => $self->param('input_hex'), 
     hex  => $input, 
     bytes => $bytes, 
     crc  => $self->crc($bytes), 
    ); 
}; 

# done 
app->start; 

__DATA__ 

@@ form.html.ep 
% layout 'default'; 
% title 'form'; 
<form action="<%= url_for 'crc' %>" method="post"> 
    <p> 
     <label for="input_hex">Hex</label>: 
     <input type="text" name="input_hex" id="input_hex"> 
     <small>(e.g : 08 13 4B 04 03 00 01 00 11)</small> 
    </p> 
    <p><input type="submit" value="calculate CRC sum!"></p> 
</form> 

@@ crc.html.ep 
% layout 'default'; 
% title 'CRC'; 
<table> 
    <tr><th>Input</th>    <td><%== $input %></td></tr> 
    <tr><th>Input (cleaned up)</th> <td><%== $hex %></td></tr> 
    <tr><th>Extracted data</th>  <td><%== $bytes %></td></tr> 
    <tr><th>CRC32</th>    <td><%== $crc %></td></tr> 
</table> 

@@ layouts/default.html.ep 
<!DOCTYPE html> 
<html><head><title>Solving berna maxim's problems: <%= title %></title> 
<style type="text/css">th{text-align:right}</style></head><body> 
%= content 
</body></html> 

從你的代碼,我看到你想用一種特殊的CRC生成多項式(0x8408)。如果你只是要檢查一些校驗,而不需要特殊的生成多項式,你可以刪除crc助手,並使用導出的CRC函數:

use Digest::CRC 'crc32'; 

... 

    $self->stash(..., crc => crc32($bytes)); 

希望有所幫助。

+0

@bernamaxim你可以表達這一點,也有助於接受和/或upvoting答案。歡迎來到StackOverflow。](http://stackoverflow.com/faq#howtoask) – memowe

+0

如果我有這樣的情況:http://sourceforge.net/projects/opengts/forums/forum/579835/topic/6157901如何編碼在Perl中?謝謝 –

+0

@bernamaxim如果您想添加更多詳細信息,請編輯該問題。如果您需要另一個問題的幫助,請提出一個新問題。如果我的答案幫助你,upvote和接受。歡迎來到StackOverflow。 – memowe