Thread Textdatei in Binärdatei umwandeln? (10 answers)
Opened by skontox at 2003-08-24 17:36

esskar
 2003-08-24 17:50
#79150 #79150
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
[quote=skontox,24.08.2003, 15:36]Wie kann ich eine Textdatei in eine Binärdatei umwandeln?
Und umgekehrt?

Gruß skontox[/quote]
hmm...
wenn du eine textdatei hast, in der echt nur text drin steht, kannst du sie einfach binaer lesen, und hast dadurch keinen datenverlust...
wenn du eine binaerdatei hast, kannst du diese nur als binaer lesen; wenn du sie als text liesst und speicherst, läufst du gefahr, daten zu verlieren...

also, wenn du eine binaer datei als textdatei speicherst, kommst du nicht mehr zur binaerdatei zurück...


du kannst es so machen


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
40
#!/usr/bin/perl

use strict;

my $file1_in  = "text.txt";
my $file1_out = "text.bin";

my $file2_in  = "bine.bin";
my $file2_out = "bine.txt";

if(open(FILE1_IN,  "< $file1_in"))
{
   my $data = join "", <FILE1_IN>;
   close(FILE1_IN);

   if(open(FILE1_OUT, "> $file1_out"))
   {
      binmode(FILE1_OUT);
      syswrite(FILE1_OUT, $data);
      close(FILE1_OUT);
   }
}

if(open(FILE2_IN,  "< $file2_in"))
{
   binmode(FILE2_IN);
   my ($len, $off, $data, $buf) = (0, 0, "", undef);
   
   while($len = sysread(FILE2_IN, $buf, 1024, $off))
   {      
      $data .= $buf; $off += $len;
   }
   close(FILE1_IN);

   if(open(FILE2_OUT, "> $file2_out"))
   {
      print FILE2_OUT $data;
      close(FILE1_OUT);
   }
}
\n\n

<!--EDIT|esskar|1061733090-->

View full thread Textdatei in Binärdatei umwandeln?