FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Traducir codigo PERL a FWH/xHarbour
Posts: 654
Joined: Mon May 29, 2006 03:14 PM
Traducir codigo PERL a FWH/xHarbour
Posted: Mon Aug 15, 2011 10:17 AM
Desear铆a que me ayudarais a traducir estos c贸digos fuente de lenguaje PERL a FWH/xHarbour.

El primero es este y las llamadas que se le hacen desde el c贸digo fuente PERL son del tipo my $rx_clr = rx_decode($rx_msg, $key1, $key2); siendo key1 y key2 los bytes de un CRC16 (eso creo).
Code (fw): Select all Collapse
sub rx_decode
{
聽 聽 my $rx_msg 聽 聽 = shift;
聽 聽 my $key1 聽 聽= shift;
聽 聽 my $key2 聽 聽= shift;

聽 聽 die "Undefined Message Bytes" 聽 聽 unless defined $rx_msg;

聽 聽 # Descramble payload bytes. 聽This is perfomed in a similar way to the
聽 聽 # encoder by calling the CRC routine initially with magic values
聽 聽 # then doing some bit shifts and logic.
聽
聽 聽 my $enc1 = 0; 聽 聽 聽 聽 聽 聽# Defaults to 0

聽 聽 my $rx_clr;

聽 聽 # Take each message byte in turn and descramble:
聽 聽 foreach my $byte (split //,$rx_msg)
聽 聽 {
聽 聽 聽 聽 $byte = ord $byte;

聽 聽 聽 聽 # Starting with the magic init values, use the returned
聽 聽 聽 聽 # CRC16 as the init values for the next round.

聽 聽 聽 聽 $key1 = crc16($key1, 0x1021, 0xFF);
聽 聽 聽 聽 $key2 = crc16($key2, 0x8005, 0xFF);

聽 聽 聽 聽 # Now some bit shifting weirdness:
聽 聽 聽 聽 my $enc2 = ($enc1 >> 3) | ($enc1 << 5);

聽 聽 聽 聽 # And some XORing for good measure:
聽 聽 聽 聽 my $enc3 = ($byte ^ $enc2) & 0xFF;
聽 聽 聽 聽 my $enc4 = ($enc3 ^ $key1) & 0xFF;
聽 聽 聽 聽 my $enc5 = ($enc4 ^ $key2) & 0xFF;
聽 聽 聽 聽 $enc1 聽 聽= ($enc2 ^ $enc5) & 0xFF;
聽 聽 聽 聽 $rx_clr .= chr $enc5;
聽 聽 }

聽 聽 return $rx_clr;
}


Dentro de este c贸digo hay una llamada a la funci贸n crc16 que es esta:
Code (fw): Select all Collapse
sub crc16
{
聽 聽 my $init = shift;
聽 聽 my $poly = shift;
聽 聽 my $val 聽= shift;

聽 聽 die "Undefined CRC value" 
聽 聽 聽 聽 unless defined $val && defined $poly && defined $init;

聽 聽 my $var;
聽 聽 for (my $i=0; $i<8; $i++)
聽 聽 {
聽 聽 聽 聽 $var = $val << 8;
聽 聽 聽 聽 $var ^= $init;
聽 聽 聽 聽 $init += $init;
聽 聽 聽 聽 $val <<= 1;

聽 聽 聽 聽 if ($var & 0x8000)
聽 聽 聽 聽 {
聽 聽 聽 聽 聽 聽 $init ^= $poly;
聽 聽 聽 聽 }
聽 聽 }

聽 聽 return $init & 0xFFFF;
}


Muchas gracias por vuestra ayuda.
Mi abuelo dec铆a: Los aviones vuelan porque Dios quiere, y los helic贸pteros ni Dios sabe porque vuelan.

FWH 16.02, xHarbour 1.2.3, Harbour 3.2.0, WorkShop 4.5, AJ Make 0.30, Borlan BCC 7.00, VisualStudio 2013

Continue the discussion