1 2
bits = pack('uint:5, uint:1, uint:10, uint:8', a, b, c, d) byte1, byte2, byte3 = bits.unpack('bytes:1,bytes:1,bytes:1')
1 2
my $bits = pack('I>5I>1I>10I>8',$a, $b, $c, $d); my @bytes = unpack('B1 B1 B1 B1', $bits);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from bitstring import Bits, pack bits = pack('uint:5, uint:1, uint:10, uint:8', 2, 0, 64, 2) byte1, byte2, byte3 = bits.unpack('bytes:1,bytes:1,bytes:1') listOfBytes = [byte1, byte2, byte3] checksum = sum(map(ord, listOfBytes)) if checksum > 511: checksum = checksum - 512 if checksum > 255: checksum = checksum - 256 if checksum > 127: checksum = checksum - 128 bits = pack('uint:5, uint:1, uint:10, uint:8, uint:8', 2, 0, 64, 2, checksum)
bits = pack('uint:5, uint:1, uint:10, uint:8', 2, 0, 64, 2)
byte1, byte2, byte3 = bits.unpack('bytes:1,bytes:1,bytes:1')
listOfBytes = bits.unpack('bytes:1,bytes:1,bytes:1')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl use warnings; use strict; my @v = qw(2 0 64 2); my @l = qw(5 1 10 8); my $i; my $bits = ""; for ($i=0; $i<=$#v; $i++) { my $s = "%0" . $l[$i] ."b"; $bits .= sprintf("$s", $v[$i]); } print "$bits\n";
2015-12-09T22:08:21 hlubenowGut. Ich benutze pack() und unpack() außerordentlich ungern und kenne mich damit daher nicht besonders gut aus.
QuoteEs werden also 4 (Integer-)Zahlen in 24 Bits umgewandelt. Diese 24 Bits werden dann auf 3 Bytes, bzw. 3 ASCII-Zeichen verteilt (24 Bits / 8 Bits pro Byte = 3 Bytes).
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/perl use warnings; use strict; sub makeBitstream { my $lenarr = $#_ + 1; if ($lenarr % 2) { die "Uneven number of elements in array."; } my @l = (); my $i; my $x = 0; while ($x < $lenarr / 2) { push(@l, shift(@_)); $x++; } my @v = @_; my $bitstream = ""; for ($i=0; $i<=$#v; $i++) { my $s = "%0" . $l[$i] ."b"; $bitstream .= sprintf("$s", $v[$i]); } return $bitstream; } sub makeThreeBytes { my $bitstream = shift; my $i; my @listOfBytes = (substr($bitstream, 0, 8), substr($bitstream, 8, 8), substr($bitstream, 16, 8)); for ($i=0; $i<=$#listOfBytes;$i++) { $listOfBytes[$i] = oct("0b". $listOfBytes[$i]); } return @listOfBytes; } my $bitstream = makeBitstream(5, 1, 10, 8, 2, 0, 64, 2); my @listOfBytes = makeThreeBytes($bitstream); my $i; my $checksum = 0; foreach $i (@listOfBytes) { $checksum += $i; } print "$checksum\n"; if ($checksum > 511) { $checksum -= 512; } if ($checksum > 255) { $checksum -= 256; } if ($checksum > 127) { $checksum -= 128; } print "$checksum\n"; my $bits = makeBitstream(5, 1, 10, 8, 8, 2, 0, 64, 2, $checksum); print "$bits\n";
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/usr/bin/perl use warnings; use strict; sub makeBitstream { my $lenarr = $#_ + 1; if ($lenarr % 2) { die "Uneven number of elements in array."; } my @l = (); my $i; my $x = 0; while ($x < $lenarr / 2) { push(@l, shift(@_)); $x++; } my @v = @_; my $bitstream = ""; for ($i=0; $i<=$#v; $i++) { my $s = "%0" . $l[$i] . "b"; $bitstream .= sprintf("$s", $v[$i]); } return $bitstream; } sub makeThreeBytes { my $bitstream = shift; my $i; my @listOfBytes = (substr($bitstream, 0, 8), substr($bitstream, 8, 8), substr($bitstream, 16, 8)); for ($i=0; $i<=$#listOfBytes;$i++) { $listOfBytes[$i] = oct("0b". $listOfBytes[$i]); } return \@listOfBytes; } sub makeChecksum { my $listOfBytes = shift; my $checksum = 0; foreach my $i (@{$listOfBytes}) { $checksum += $i; } # print "checksum1: $checksum\n"; $checksum -= 512 if ($checksum > 511); $checksum -= 256 if ($checksum > 255); $checksum -= 128 if ($checksum > 127); # print "checksum1: $checksum\n"; return $checksum; } sub makeCommandStream { my @params = @_; my @a = ((5, 1, 10, 8), @params); my $bitstream = makeBitstream(@a); my $listOfBytes = makeThreeBytes($bitstream); push @params, makeChecksum($listOfBytes); my @b = ((5, 1, 10, 8, 8), @params); return makeBitstream(@b); } print makeCommandStream(2, 0, 64, 2) . "\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#! /usr/bin/perl use strict; use warnings; # convert values into bitstring (check sizes before!) my $bits = sprintf( "%05b%01b%010b%08b", 2, 0, 64, 2 ); print $bits, "\n"; # split bitstring into bitstring per byte (length of $bits MUST be a multiple of 8) my @bytes = $bits =~ m/([01]{8})/g; print "String: $_\n" for @bytes; # convert bitstrings to numbers @bytes = map { oct "0b$_" } @bytes; print "Number: $_\n" for @bytes;