Dennis9
asked on
Open file as: 10101001101....?
Hi.
How can i open a file as integer:101011010100011110 001010111 ???
How can i put the file together again, and save it??
It should be able to be displayed as a string (inttostr).
Thanks.
Dennis
How can i open a file as integer:101011010100011110
How can i put the file together again, and save it??
It should be able to be displayed as a string (inttostr).
Thanks.
Dennis
ASKER
Yes, but can they be converted to integer or string?
Dennis
Dennis
just from head, not tested
var
f : file of integer;
i : Integer;
begin
assignfile(f,AFilename);
rewrite(f);
for i := 0 to 50 do
write(f,I);
closefile(f);
reset(f);
seek(f,25); //read the 26th Integervalue
read(f,i);
showmessage(IntToStr(i);
closefile(f);
end;
meikl ;-)
var
f : file of integer;
i : Integer;
begin
assignfile(f,AFilename);
rewrite(f);
for i := 0 to 50 do
write(f,I);
closefile(f);
reset(f);
seek(f,25); //read the 26th Integervalue
read(f,i);
showmessage(IntToStr(i);
closefile(f);
end;
meikl ;-)
Hi,
here are 3 functions to convert a byte, word and dword
to a string of BITS and 1 function to convert a string
of BITS to INTEGER.
I don't know if it that what you want.
Function ByteToBit(b : byte) : String;
Var
z : byte;
Begin
Result := '';
For z := 1 to 8 do
Begin
IF b AND 128 = 128 then Result := Result + '1'
else Result := Result + '0';
b := b SHL 1;
End;
End;
Function WordToBit(w : word) : string;
Var
z : byte;
Begin
Result := '';
For z := 1 to 16 do
Begin
IF w AND 32768 = 32768 then Result := Result + '1'
else Result := Result + '0';
w := w SHL 1;
End;
End;
Function DWordToBit(d : dword) : string;
Var
z : byte;
Begin
Result := '';
For z := 1 to 32 do
Begin
IF d AND $80000000 = $80000000 then Result := Result + '1'
else Result := Result + '0';
d := d SHL 1;
End;
End;
Function BitsToInt(s : string) : Integer;
Var
z : byte;
Begin
Result := 0;
For z := 1 to Length(s) do
Begin
IF s[z] = '1' then Result := Result +1;
IF z <> Length(s) then Result := Result SHL 1;
End;
End;
here are 3 functions to convert a byte, word and dword
to a string of BITS and 1 function to convert a string
of BITS to INTEGER.
I don't know if it that what you want.
Function ByteToBit(b : byte) : String;
Var
z : byte;
Begin
Result := '';
For z := 1 to 8 do
Begin
IF b AND 128 = 128 then Result := Result + '1'
else Result := Result + '0';
b := b SHL 1;
End;
End;
Function WordToBit(w : word) : string;
Var
z : byte;
Begin
Result := '';
For z := 1 to 16 do
Begin
IF w AND 32768 = 32768 then Result := Result + '1'
else Result := Result + '0';
w := w SHL 1;
End;
End;
Function DWordToBit(d : dword) : string;
Var
z : byte;
Begin
Result := '';
For z := 1 to 32 do
Begin
IF d AND $80000000 = $80000000 then Result := Result + '1'
else Result := Result + '0';
d := d SHL 1;
End;
End;
Function BitsToInt(s : string) : Integer;
Var
z : byte;
Begin
Result := 0;
For z := 1 to Length(s) do
Begin
IF s[z] = '1' then Result := Result +1;
IF z <> Length(s) then Result := Result SHL 1;
End;
End;
ASKER
Hi.
I just need a way to open a file, and have it as an integer or string, and then i should be able to save the string/integer back to file. (copy of it)
Dennis
I just need a way to open a file, and have it as an integer or string, and then i should be able to save the string/integer back to file. (copy of it)
Dennis
like a hex editor?
Why don't you use TFileStream. You can open the file as a stream, then you can read bytes from it into a buffer and do anything you want with the buffer.
Does this help?
Eric
Does this help?
Eric
ASKER
I will like it as text, but if u can tell me more about the buffer stuff it would be great!
Dennis
Dennis
Well, a lot of using a stream is knowing what the data you are reading in will look like. Do you have a good idea of what you are reading in?
Here are some examples.
Reading an integer:
After defining MyIntBuffer as an Integer, you can execute the following code to read an integer from the stream.
MyFileStream.Read(MyIntBuf fer, SizeOf(MyIntBuffer));
Reading a string:
After defining MyCharBuffer as an Array of Char, you can execute the following code to read a string from the stream. The catch is that you need to know how much to read from the stream.
MyFileStream.Read(MyCharBu ffer, NumCharsToRead);
Maybe this will work for you and maybe it's too much work. You have to decide that. :)
Eric
Here are some examples.
Reading an integer:
After defining MyIntBuffer as an Integer, you can execute the following code to read an integer from the stream.
MyFileStream.Read(MyIntBuf
Reading a string:
After defining MyCharBuffer as an Array of Char, you can execute the following code to read a string from the stream. The catch is that you need to know how much to read from the stream.
MyFileStream.Read(MyCharBu
Maybe this will work for you and maybe it's too much work. You have to decide that. :)
Eric
ASKER
Hi.
I need to read all from the stream, is there a way to do that?
Dennis
I need to read all from the stream, is there a way to do that?
Dennis
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"open a file as integer: 101010101010101010?"
do you mean as bits?