1 2 3
my $i = "\x32\x10"; printf "%02x\n", unpack('n', $i); # 3210 printf "%02x\n", unpack('v', $i); # 1032
sub swap_bytes { shift; pack(q{v*},unpack(q{n*},$_)) }
Guest piet32 Bit PC = Byte 32 10
benötigt = Byte 01 23
Guest pietIch habe eine 32 Bit Zahl ... also 4 Byte (0..3)
z.B.
Byte 3210
Guest pietByte 3210
als Ergebnis soll ein Byte und Wort-Tausch statfinden
Byte 0123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#! /usr/bin/env perl use strict; use warnings; # 32bit data my $buf = 0x12345678; # reverse byte order in 32bit value my $buf2 = unpack( 'L', reverse pack L => $buf ); # show me the bytes in hex: my $FORMAT = ( "%02x " x 4 ) . "\n"; printf $FORMAT, unpack( "W*", pack L => $buf ); printf $FORMAT, unpack( "W*", pack L => $buf2 ); __END__ Output: 78 56 34 12 12 34 56 78