FHEM Forum

FHEM - Entwicklung => FHEM Development => Perl Ecke => Thema gestartet von: amenomade am 17 Mai 2020, 20:50:47

Titel: String To Bytes eleganter?
Beitrag von: amenomade am 17 Mai 2020, 20:50:47
Hallo Perl Experten

Ich habe es so gelöst, aber es gibt bestimmt eine bessere Art (z.B. mit unpack oder split?) :
sub
stringToBytes
{
   my ($input) = @_;
   my @output;
   for (my $i = 0; $i < length($input); $i++) {
      push @output, ord(substr($input, $i, 1));
   }
   return @output;
}

(ja, ich weiss, @_ => shift... ;) )
Titel: Antw:String To Bytes eleganter?
Beitrag von: Sidey am 17 Mai 2020, 21:12:48
Sicher, geht das auch ohne Schleife:

https://docstore.mik.ua/orelly/perl4/cook/ch01_05.htm

Aus der Bezeichnung string2byte geht für mich aber nicht hervor um was für eine String es sich handelt.

Grüße Sidey

Gesendet von meinem Moto Z (2) mit Tapatalk

Titel: Antw:String To Bytes eleganter?
Beitrag von: amenomade am 17 Mai 2020, 22:27:40
Die String ist das Ergebnis von einer AES-Entzifferung und enthält möglicherweise nicht-druckbare Zeichen

Dies sieht interessant aus:
@ascii_character_numbers = unpack("C*", "sample"); print "@ascii_character_numbers\n";
Ich werde testen.

Ob unpack C und ord das gleiche machen, ist die Frage.

Vielen Dank!
Titel: Antw:String To Bytes eleganter?
Beitrag von: amenomade am 17 Mai 2020, 22:45:02
Jepp! Funktioniert!
output = 35 39 58 39 44 60 58 95 37 37 0 0 0 33 45 21 93 15 26 26 43 0 9 0 0 127 121 127 121 33 1 1 1 1 5 20 70 6 66 16 0 0 10 0 0 1 33 184
Unpack = 35 39 58 39 44 60 58 95 37 37 0 0 0 33 45 21 93 15 26 26 43 0 9 0 0 127 121 127 121 33 1 1 1 1 5 20 70 6 66 16 0 0 10 0 0 1 33 184

Vielen Dank nochmal :)
Titel: Antw:String To Bytes eleganter?
Beitrag von: herrmannj am 17 Mai 2020, 22:52:54
https://catonmat.net/ftp/perl.pack.unpack.printf.cheat.sheet.pdf