Thread alle byte im 32Bit vertauschen (11 answers)
Opened by piet at 2013-12-19 18:20

Linuxer
 2013-12-29 02:27
#172748 #172748
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Du hast also einen 4-Byte großen Wert, und Du willst die Reihenfolge der Bytes umkehren?
Da sind Perldoc:perlfunc pack und Perldoc:perlfunc unpack immer noch die Tools der Wahl. Siehe auch Perldoc:perlpacktut.
Und da es Verwendung findet, auch noch: Perldoc:perlfunc reverse


Vorschlag:

Code (perl): (dl )
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


Achtung: Vielleicht bringen andere Benutzer noch eine andere oder bessere Lösung. Ich nutze pack/unpack selbst recht selten.
Last edited: 2013-12-29 17:24:10 +0100 (CET)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread alle byte im 32Bit vertauschen