Thread ByteArray in Perl abbilden (Code aus Python portieren)
(4 answers)
Opened by prodigy7 at 2016-11-21 22:13
ohne es getestet zu haben:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 use integer; my @datetime = localtime(); my $year = $datetime[5]+1900; my $timezone = 1; my $port = 80; my @address=(192,168,0,42); my @data = ( (0) x 39 ); if($timezone < 0 ) { $data[8] = 255 + $timezone - 1; @data[9,10,11] = ( 255,255,255 ); } else { $data[8] = $timezone; @data[9,10,11] = ( 0,0,0 ); } $data[12] = $year & 255; $data[13] = $year >> 8; $data[14] = $datetime[1]; $data[15] = $datetime[2]; $data[16] = int(substr($year,2)); $data[17] = $datetime[6]; $data[18] = $datetime[3]; $data[19] = $datetime[4]+1; $data[24] = int($address[0]); $data[25] = int($address[1]); $data[26] = int($address[2]); $data[27] = int($address[3]); $data[28] = $port & 255; $data[29] = $port >> 8; $data[38] = 6; my $packet = pack('C*',@data); und das wieder in ein Array wandeln: Code (perl): (dl
)
my @data = unpack('C*',$packet); Wobei es sinnvoll wäre das ganze ">>" und "&" weg zu lassen und Perl das zu überlassen in dem man die Datensätze passend definiert: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 my @datetime = localtime(); my $year = $datetime[5]+1900; my $timezone = 1; my $port = 80; my @address=(168,191,0,1); my @data = ( (0) x 37 ); my @form = ( ('C') x 37 ); if($timezone < 0 ) { $data[8] = 255 + $timezone - 1; @data[9,10,11] = ( 255,255,255 ); } else { $data[8] = $timezone; @data[9,10,11] = ( 0,0,0 ); } $data[12] = $year; $form[12] = 'v'; $data[13] = $datetime[1]; $data[14] = $datetime[2]; $data[15] = int(substr($year,2)); $data[16] = $datetime[6]; $data[17] = $datetime[3]; $data[18] = $datetime[4]+1; $data[23] = int($address[0]); $data[24] = int($address[1]); $data[25] = int($address[2]); $data[26] = int($address[3]); $data[27] = $port; $form[27] = 'v'; $data[36] = 6; my $packet = pack(join('',@form),@data); Last edited: 2016-11-22 00:36:03 +0100 (CET) |