• 15 odgovora
  • 6.249 pregleda
Binarni zapis fajla 10.04.2008. u 21:36 · pre 225 meseci #1
Jel moze neko da mi kaze kako da iscitam binarni zapis nekog fajla?
Pozz
Re: Binarni zapis fajla 11.04.2008. u 08:36 · pre 225 meseci #2
> Jel moze neko da mi kaze kako da iscitam binarni zapis nekog fajla?

Verovatno će ti biti najlakše preko TFileStream. Fajl otvaraš sa TFileStream.Create('Putanja', fmOpenRead or fmShareDenyNone);
Potpis
Do not be a help vampire (http://slash7.com/pages/vampires), use the google.

How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html
http://solair.eunet.rs/~fangor...stions/smart-questions.sr.html
Re: Binarni zapis fajla 11.04.2008. u 08:51 · pre 225 meseci #3
Citat:
GyG@: Jel moze neko da mi kaze kako da iscitam binarni zapis nekog fajla?


Ja sam tvoje pitanje shvatio na sasvim drugi način, od onog na koji ga je shvatio Igor. Meni se čini da želiš prikaz fajla kao hex dump, a možda i grešim. Verovatno će neko ovo da protumači na neki treći način.

Zato, budi malo konkretniji.

Šta u stvari hoćeš da uradiš?

Potpis
viking ®
Re: Binarni zapis fajla 14.04.2008. u 19:35 · pre 225 meseci #5
Citat:
savkic: > Jel moze neko da mi kaze kako da iscitam binarni zapis nekog fajla?

Verovatno će ti biti najlakše preko TFileStream. Fajl otvaraš sa TFileStream.Create('Putanja', fmOpenRead or fmShareDenyNone);

Jel moze neki primer?

Znaci treba mi da ucitam fajl,zatim ga preveden u binarni zapis i na kraju ga snimim kao novi fajl...
Pozz
Re: Binarni zapis fajla 15.04.2008. u 00:52 · pre 225 meseci #6
> Znaci treba mi da ucitam fajl,zatim ga preveden u binarni zapis i na kraju ga snimim kao novi fajl...

Razjasni šta to znači, koji fajl imaš i kako treba da ga konvertuješ.
Potpis
Do not be a help vampire (http://slash7.com/pages/vampires), use the google.

How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html
http://solair.eunet.rs/~fangor...stions/smart-questions.sr.html
Re: Binarni zapis fajla 15.04.2008. u 08:54 · pre 225 meseci #7
Citat:
savkic: > Znaci treba mi da ucitam fajl,zatim ga preveden u binarni zapis i na kraju ga snimim kao novi fajl...

Razjasni šta to znači, koji fajl imaš i kako treba da ga konvertuješ.


Ne bi me začudilo da na kraju ispadne da hoće da iskopira fajl.
Potpis
viking ®
Re: Binarni zapis fajla 15.04.2008. u 09:38 · pre 225 meseci #8
Imam fajl(bilo koji:slika,zvuk,app...)i treba da ga zapisem u obliku jedinica i nula...Valjda je dovoljno jasno...
Pozz
Re: Binarni zapis fajla 15.04.2008. u 09:48 · pre 225 meseci #9
Hoces da neki fajl pretvoris u text fajl koji bi imao slova/cifre '0' i '1' na mestu bitova originalnog fajla?
Re: Binarni zapis fajla 15.04.2008. u 12:35 · pre 225 meseci #11
Konverzija fajla u binarne brojeve:
Code:
function FileToBin(const ASourceFile, ATargetFile : String) : Integer;

  function IntToBin(AValue : LongInt; ADigits : Integer) : String;
  begin
    result := StringOfChar('0', ADigits);
    While AValue > 0 Do
    Begin
      If (AValue and 1) = 1 Then
        result[ADigits] := '1';
      Dec(ADigits);
      AValue := AValue shr 1;
    End;
  end;

type
  TCharArray = Array[0..High(Word) div SizeOf(Char) - 1] of Char;
  PCharArray = ^TCharArray;

const
  maxbufsize = 1024 * 1024 * 16; // 16mb
var
  fhndIn, fhndOut : HFILE;
  ofstr           : TOfStruct;
  buf, binbuf     : PCharArray;
  bread, bwrite   : DWORD;
  C1              : DWORD;
  binstr          : String;
  bufsize         : DWORD;
  filesize        : DWORD;
begin
  fhndIn := OpenFile(PAnsiChar(ASourceFile), ofstr, OF_READ);
  If fhndIn <> HFILE_ERROR Then
  Begin
    fhndOut := CreateFile(PAnsiChar(ATargetFile), GENERIC_READ or GENERIC_WRITE, 0, nil,
                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    If fhndOut <> INVALID_HANDLE_VALUE Then
    Begin
      filesize := GetFileSize(fhndIn, nil);
      If filesize > maxbufsize Then
        bufsize := maxbufsize
      else
        bufsize := filesize;

      GetMem(buf, bufsize);
      GetMem(binbuf, bufsize * 8);
      repeat
        ReadFile(fhndIn, buf^, bufsize, bread, nil);
        If bread > 0 Then
          For C1 := 0 to bread - 1 Do
          Begin
            binstr := IntToBin(Ord(buf^[C1]), 8);
            Move(binstr[1], binbuf^[C1 * 8], 8);
          End;
        WriteFile(fhndOut, binbuf^, bread * 8, bwrite, nil);
      until bufsize <> bread;
      FreeMem(buf);
      FreeMem(binbuf);
      CloseHandle(fhndOut);
      result := ERROR_SUCCESS;
    End
    else
      result := GetLastError;

    CloseHandle(fhndIn);
  End
  else
    result := GetLastError;
end;


Konverzija fajla koji se sastoji od binarnih brojeva u normalan oblik:
Code:
function BinToFile(const ASourceFile, ATargetFile : String) : Integer;

  function BinToInt(AValue : String) : LongInt;
  var
    C1  : Integer;
    len : Integer;
    cut : Integer;
  begin
    result := 0;

    len := Length(AValue);
    cut := 0;
    For C1 := 1 to Length(AValue) Do
      If AValue[C1] <> '0' Then
      Begin
        cut := C1 - 1;
        Break;
      End;
    If cut > 0 Then
    Begin
      Delete(AValue, 1, cut);
      Dec(len, cut);
    End;

    If AValue <> '' Then
      For C1 := len downto 1 Do
        If AValue[C1] = '1' Then
          result := result + (1 shl (len - C1));
  end;

type
  TCharArray = Array[0..High(Word) div SizeOf(Char) - 1] of Char;
  PCharArray = ^TCharArray;

const
  maxbufsize = 1024 * 1024 * 16; // 16mb
var
  fhndIn, fhndOut : HFILE;
  ofstr           : TOfStruct;
  buf, chrbuf     : PCharArray;
  bread, bwrite   : DWORD;
  bufsize         : DWORD;
  filesize        : DWORD;
  C1              : Integer;
begin
  fhndIn := OpenFile(PAnsiChar(ASourceFile), ofstr, OF_READ);
  If fhndIn <> HFILE_ERROR Then
  Begin
    fhndOut := CreateFile(PAnsiChar(ATargetFile), GENERIC_READ or GENERIC_WRITE, 0, nil,
                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    If fhndOut <> INVALID_HANDLE_VALUE Then
    Begin
      filesize := GetFileSize(fhndIn, nil);
      If filesize > maxbufsize Then
        bufsize := maxbufsize
      else
        bufsize := filesize;

      GetMem(buf, bufsize);
      GetMem(chrbuf, bufsize div 8);
      repeat
        ReadFile(fhndIn, buf^, bufsize, bread, nil);
        If bread > 0 Then
          For C1 := 0 to (bread div 8) - 1 Do
            chrbuf^[C1] := Chr(BinToInt(Copy(buf^, C1 * 8 + 1, 8)));

        WriteFile(fhndOut, chrbuf^, bread div 8, bwrite, nil);
      until bufsize <> bread;
      FreeMem(buf);
      FreeMem(chrbuf);
      CloseHandle(fhndOut);
      result := ERROR_SUCCESS;
    End
    else
      result := GetLastError;

    CloseHandle(fhndIn);
  End
  else
    result := GetLastError;
end;


Btw, BinToFile() radi mnogo sporo za velike fajlove, recimo za fajl od 175mb mu treba oko 5-10 minuta da ga vrati u normalan oblik.
Re: Binarni zapis fajla 16.04.2008. u 19:05 · pre 225 meseci #12
Citat:
reiser: Konverzija fajla u binarne brojeve:
Code:
function FileToBin(const ASourceFile, ATargetFile : String) : Integer;

  function IntToBin(AValue : LongInt; ADigits : Integer) : String;
  begin
    result := StringOfChar('0', ADigits);
    While AValue > 0 Do
    Begin
      If (AValue and 1) = 1 Then
        result[ADigits] := '1';
      Dec(ADigits);
      AValue := AValue shr 1;
    End;
  end;

type
  TCharArray = Array[0..High(Word) div SizeOf(Char) - 1] of Char;
  PCharArray = ^TCharArray;

const
  maxbufsize = 1024 * 1024 * 16; // 16mb
var
  fhndIn, fhndOut : HFILE;
  ofstr           : TOfStruct;
  buf, binbuf     : PCharArray;
  bread, bwrite   : DWORD;
  C1              : DWORD;
  binstr          : String;
  bufsize         : DWORD;
  filesize        : DWORD;
begin
  fhndIn := OpenFile(PAnsiChar(ASourceFile), ofstr, OF_READ);
  If fhndIn <> HFILE_ERROR Then
  Begin
    fhndOut := CreateFile(PAnsiChar(ATargetFile), GENERIC_READ or GENERIC_WRITE, 0, nil,
                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    If fhndOut <> INVALID_HANDLE_VALUE Then
    Begin
      filesize := GetFileSize(fhndIn, nil);
      If filesize > maxbufsize Then
        bufsize := maxbufsize
      else
        bufsize := filesize;

      GetMem(buf, bufsize);
      GetMem(binbuf, bufsize * 8);
      repeat
        ReadFile(fhndIn, buf^, bufsize, bread, nil);
        If bread > 0 Then
          For C1 := 0 to bread - 1 Do
          Begin
            binstr := IntToBin(Ord(buf^[C1]), 8);
            Move(binstr[1], binbuf^[C1 * 8], 8);
          End;
        WriteFile(fhndOut, binbuf^, bread * 8, bwrite, nil);
      until bufsize <> bread;
      FreeMem(buf);
      FreeMem(binbuf);
      CloseHandle(fhndOut);
      result := ERROR_SUCCESS;
    End
    else
      result := GetLastError;

    CloseHandle(fhndIn);
  End
  else
    result := GetLastError;
end;


Konverzija fajla koji se sastoji od binarnih brojeva u normalan oblik:
Code:
function BinToFile(const ASourceFile, ATargetFile : String) : Integer;

  function BinToInt(AValue : String) : LongInt;
  var
    C1  : Integer;
    len : Integer;
    cut : Integer;
  begin
    result := 0;

    len := Length(AValue);
    cut := 0;
    For C1 := 1 to Length(AValue) Do
      If AValue[C1] <> '0' Then
      Begin
        cut := C1 - 1;
        Break;
      End;
    If cut > 0 Then
    Begin
      Delete(AValue, 1, cut);
      Dec(len, cut);
    End;

    If AValue <> '' Then
      For C1 := len downto 1 Do
        If AValue[C1] = '1' Then
          result := result + (1 shl (len - C1));
  end;

type
  TCharArray = Array[0..High(Word) div SizeOf(Char) - 1] of Char;
  PCharArray = ^TCharArray;

const
  maxbufsize = 1024 * 1024 * 16; // 16mb
var
  fhndIn, fhndOut : HFILE;
  ofstr           : TOfStruct;
  buf, chrbuf     : PCharArray;
  bread, bwrite   : DWORD;
  bufsize         : DWORD;
  filesize        : DWORD;
  C1              : Integer;
begin
  fhndIn := OpenFile(PAnsiChar(ASourceFile), ofstr, OF_READ);
  If fhndIn <> HFILE_ERROR Then
  Begin
    fhndOut := CreateFile(PAnsiChar(ATargetFile), GENERIC_READ or GENERIC_WRITE, 0, nil,
                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    If fhndOut <> INVALID_HANDLE_VALUE Then
    Begin
      filesize := GetFileSize(fhndIn, nil);
      If filesize > maxbufsize Then
        bufsize := maxbufsize
      else
        bufsize := filesize;

      GetMem(buf, bufsize);
      GetMem(chrbuf, bufsize div 8);
      repeat
        ReadFile(fhndIn, buf^, bufsize, bread, nil);
        If bread > 0 Then
          For C1 := 0 to (bread div 8) - 1 Do
            chrbuf^[C1] := Chr(BinToInt(Copy(buf^, C1 * 8 + 1, 8)));

        WriteFile(fhndOut, chrbuf^, bread div 8, bwrite, nil);
      until bufsize <> bread;
      FreeMem(buf);
      FreeMem(chrbuf);
      CloseHandle(fhndOut);
      result := ERROR_SUCCESS;
    End
    else
      result := GetLastError;

    CloseHandle(fhndIn);
  End
  else
    result := GetLastError;
end;


Btw, BinToFile() radi mnogo sporo za velike fajlove, recimo za fajl od 175mb mu treba oko 5-10 minuta da ga vrati u normalan oblik.


E bas to mi treba!!!Hvala ti!
Samo imam problem kada prebacim recimo neku sliku u binarni zapis,a zatim pokusam da je vratim u prvobitno stanje,iscrta mi samo pola slike...A kad je rec o aplikacijama,onda nece da ih otvori...
Jel zna neko u cemu je problem?
Pozz

Re: Binarni zapis fajla 16.04.2008. u 20:44 · pre 225 meseci #13
Da li je ovo akademski problem (rešavaš postavljeni zadatak) ili je konkretni (komercijalni) program? Pitam jer ima boljih načina za tekstualnu reprezantaciju fajla poput BASE64/UUENCODE.
Potpis
Do not be a help vampire (http://slash7.com/pages/vampires), use the google.

How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html
http://solair.eunet.rs/~fangor...stions/smart-questions.sr.html
Re: Binarni zapis fajla 16.04.2008. u 21:33 · pre 225 meseci #14
@GyG@
Hm, ima neki bug u decode fji.. a nemam mozga da ga nadjem sad... 'Ajde javi da li ti bas treba binarni zapis pa da prepravim tu fju ili ti treba nesto kao sto je savkic predlozio :)
I da, ova decode fja radi uzasno sporo, enkodovanu sliku od 300kb dekoduje ceo minut xD
Re: Binarni zapis fajla 20.04.2008. u 21:11 · pre 225 meseci #15
Citat:
savkic: Da li je ovo akademski problem (rešavaš postavljeni zadatak) ili je konkretni (komercijalni) program? Pitam jer ima boljih načina za tekstualnu reprezantaciju fajla poput BASE64/UUENCODE.

Nasao sam neku komponentu koja radi sa BASE64.Zove se SZCodeBaseX.Samo jos da je skontam... :)

Citat:
reiser: @GyG@
Hm, ima neki bug u decode fji.. a nemam mozga da ga nadjem sad... 'Ajde javi da li ti bas treba binarni zapis pa da prepravim tu fju ili ti treba nesto kao sto je savkic predlozio :)
I da, ova decode fja radi uzasno sporo, enkodovanu sliku od 300kb dekoduje ceo minut xD

Da,skontao sam da je spora,tako da cu pokusati sa BASE64....

Hvala puno!
Pozz
Re: Binarni zapis fajla 21.04.2008. u 00:33 · pre 225 meseci #16
> Nasao sam neku komponentu koja radi sa BASE64.Zove se SZCodeBaseX.Samo jos da je skontam... :)

Skini JCL (www.delphi-jedi.org) i u JclMime.pas ćeš naći encode funkcije. Ako ti je potreban veći izbor enkoding metoda i veći broj opcija onda možeš koristiti Indy, uniti su IdCoder*.
Potpis
Do not be a help vampire (http://slash7.com/pages/vampires), use the google.

How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html
http://solair.eunet.rs/~fangor...stions/smart-questions.sr.html
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme