Reading Binary file, the code change bytes

F

Freddy Coal

Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.

I make this function for make that:

'--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
dato = My.Computer.FileSystem.ReadAllBytes(ruta)
For Each dat As Byte In dato
cadena = cadena & Microsoft.VisualBasic.ChrW(dat)
Next
Return cadena
Else
Return "Error"
End If
End Function
'--------------------------

Well, my problem is that in the final string, some bytes in the string not
are the originals, How can correct that?.

Thanks in advance for any help.

Freddy Coal.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Freddy said:
Patrice, each 4 bytes in the file are a number for my. I supose that each
byte is a char, because 1 char have 8bit, and the ASCII have 255 characters;
I need obtain the char, take that, join 4 bytes and get the number.

http://msdn2.microsoft.com/en-us/library/60ecse8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/9hxt0028(VS.80).aspx

You are confusing the concepts of text, binary data, code pages, and
encoding.

When text is stored in a file, it's encoded into bytes, where each
character is represented by one or more bytes. You are reading the file
under the assumption that each character is always only a single byte,
which might not be true at all.

The ASCII encoding only contains 128 characters, the character tables
you are referring to is generally called "extended ASCII". It's not an
encoding, so the characters above 127 also depends on the codepage used
by the application, so they might not at all be the ones shown in the table.

If you want to handle the data as bytes, you should not try to convert
it into a string at all. A string in .NET is unicode, so a Char is 16
bits, not 8 bits.

You might want to look into the BinaryReader class if you wish to read
binary data that represent something other than just byte values.
 
F

Freddy Coal

Göran, nice apreciation, but if you read my code, you can see that I read
byte peer byte for make the string.

The instrument send me an string, and I don´t explain how you say me, that
each chars in the String have 16bits (maybe you confuse the size of the char
variable), Try to put binary data in a file, and see the size of the file,
you can see the number of bytes, never is the double of your bytes.

Well, my real problem is that, the instrument send me binary data (4 bytes
peer value), and I need recover that, and I need the number that represent a
byte, the number for each byte is between 0 and 255.

Thanks a lot Göran.

My best regards.

Freddy Coal
 
P

Patrice

On the other hand, if you save a text file using notepad as unicode you'll
see that you have more bytes than characters. There is nothing such as a
text file. All files are a set of bytes and reading this file to a string
may depends upon how this text is encoded (try to save the same file as
"ansi"," unicode", "utf-8" with notepad and see if they are the same,
despite the text being the same).

My approach would be :
- is the string roughly what you expect or it is totally different ?
- what if you open this file with notepad

1) You have something that you can't recognize.

As you said that you expect numeric values with 4 bytes, it's likely that
those values are a series of int32 values in which case you would read them
using a BinaryReader as suggested by Göran.

2) It looks like what you expect with minor differences

If this is really a string it could for example return the string length,
followed by the ASCII characters, then perhaps another string and so on, in
which case the length encoded in the result could introduce "dummy"
characters in the resulting string.

The problem is that this is not specifically a programming error. If we
don't know what the device is expected to return exactly we won't be able to
help. Don't you have a doucmentation that would describe what this device is
supposed to return ?

If each byte looks like a random value between 0 and 255, it doesn'( llok
like text to me. It would be rather case 1 that is a serires of integers...

Good luck.

---
Patrice


Freddy Coal said:
Göran, nice apreciation, but if you read my code, you can see that I read
byte peer byte for make the string.

The instrument send me an string, and I don´t explain how you say me, that
each chars in the String have 16bits (maybe you confuse the size of the
char variable), Try to put binary data in a file, and see the size of the
file, you can see the number of bytes, never is the double of your bytes.

Well, my real problem is that, the instrument send me binary data (4 bytes
peer value), and I need recover that, and I need the number that represent
a byte, the number for each byte is between 0 and 255.

Thanks a lot Göran.

My best regards.

Freddy Coal
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Freddy said:
Göran, nice apreciation, but if you read my code, you can see that I read
byte peer byte for make the string.

Yes, I saw that, and that is the reason for your problems. You read the
bytes and convert to characters, without knowing what encoding is used
for the conversion, then you try to use the characters as if they still
were bytes.
The instrument send me an string, and I don´t explain how you say me, that
each chars in the String have 16bits (maybe you confuse the size of the char
variable), Try to put binary data in a file, and see the size of the file,
you can see the number of bytes, never is the double of your bytes.

The data in the file is not a string. It may be the encoded
representation of a string, or it may be just binary data. A string in
..NET is made up of an array of Char values, and each Char is a 16 bit
value. If you decode the data in the file into a string, one (or more)
bytes from the file will be decoded into one Char value.
Well, my real problem is that, the instrument send me binary data (4 bytes
peer value), and I need recover that, and I need the number that represent a
byte, the number for each byte is between 0 and 255.

As you want to read the binary data, you should not try to convert it to
a string at all.

You have to know how the four bytes form a value to be able to read
them. There are generally four possible values:

little endian, unsigned
little endian, signed
big endian, unsigned
big endian, signed

If the form is little endian, you can use a BinaryReader to read the
values. ReadUInt32 reads an unsigned value, and ReadInt32 reads a signed
value.

If the form is big endian, I would just read the data backwards by
reading it into a byte array, use the Reverse method to reverse it,
create a MemoryStream from the array and use a BinaryReader to read from
it.

On endianness:
http://en.wikipedia.org/wiki/Endianness
 
B

Branco Medeiros

Freddy Coal wrote:
<backposted/>

Freddy, it would help if you posted both the string you get from the
method as well as the kind of input you are reading, in this case, the
contents of the binary file. Nevertheless, the main question is, how
can you tell both are different?

Regards,

Branco.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Freddy said:
Hi Branco, thanks for your answer, I attach the original binary file and
other file that represent the value of each 4 bytes in the trace.

The binary have a offset of 6 bytes, because he have a header (#42404).

#4 -> Number of bytes of the size trace descriptor.
2404 -> Number of trace bytes, where each data is represent by 4bytes =
2404/4 = 601 values

Well, I need put all in a string again, the origin of the binary file is a
string, other program take a trace like string and save that in a file.

My question is how obtain the string, I don't have other question, I don't
need other explanation, I don't need other solution.

Thanks a lot if you can help me.

Freddy Coal.

Seing the actual data does answer some questions.

The numbers are stored as big endian, unsigned. Use my suggestion from
my previous post to read the file backwards.

Examle code (in C#, but you should be able to see the method):

byte[] data = File.ReadAllBytes("INT32 Trace1.fc");
data.Reverse();
MemoryStream m = new MemoryStream(data);
BinaryReader reader = new BinaryReader(m);
int[] numbers = new int[601];
for (int i=0; i<601; i++) {
numbers = reader.ReadInt32();
}
reader.Dispose();
m.Dispose();
numbers.Reverse();

Now you have an array of integer values. To write them out as a text
file, use ToString on each value to get the text representation of the
number.
 
B

Branco Medeiros

<snip>

I never saw this post -- and actually lost a previous one on this
subject. What else did I lose the last time Google had the fits...?

Whatever.

Freddy, I suggest you post us at least a few lines from an hex dump of
the binary file, so we can be in the same level here.

If you don't have an hex editor or viewer (like the one that comes
with Notepad++ for example), you can easily (ahem) get an hex listing
of the file like so:

1) go to the cmd prompt

2) navigate to the folder where your binary file lives (CD \folder\of
\the\binary\file)

3) copy the file to myfile.dat ( copy yourbinaryfilename myfile.dat )
-- this would be necessary only if your file has a long name or an
extension with more than three chars, but do it anyway, to make this
how-to easier to follow.

4) type the following (and hit enter)

debug myfile.dat

5) type d and hit enter. A dump of the first bytes of the file will
appear

6) type q (and hit enter).

7) Breath.

8) Select the dump data, copy it to a message and post it here.

The dump data will look more or less like this (wordwrap
notwithstanding):

13E5:0100 25 50 44 46 2D 31 2E 31-0A 25 E2 E3 CF D3 0D 0A
%PDF-1.1.%......
13E5:0110 32 20 30 20 6F 62 6A 0A-3C 3C 0A 2F 4C 65 6E 67 2 0
obj.<<./Leng
13E5:0120 74 68 20 34 35 37 36 0A-2F 46 69 6C 74 65 72 20 th 4576./
Filter
13E5:0130 2F 4C 5A 57 44 65 63 6F-64 65 0A 3E 3E 0A 73 74 /
LZWDecode.>>.st
13E5:0140 72 65 61 6D 0D 0A 80 10-8A 80 A1 79 18 68 20 18
ream.......y.h .
13E5:0150 88 0A 86 60 50 C4 68 2E-19 8D 06 C2 01 84 52
11 ...`P.h.......R.
13E5:0160 0F 88 C4 C6 23 71 88 B8-72 20 1B 0D 87 22 E1
94 ....#q..r ..."..
13E5:0170 1C A8 6D 05 45 4C E0 A2-11 60 40 2F 23 94 E1
26 ..m.EL...`@/#..&

This will certainly give us a more accurate view of the problem.

Regards,

Branco.
 
A

AlexS

Freddy,

you can try this

myString = System.Text.Encoding.GetEncoding(28591).GetString(data)

where data is byte[]. You read your file in data with ReadAllBytes as you do
now

Alex
 
F

Freddy Coal

Alex, Thanks a lot, work good for me, I change 28591 for 0 that is the
default encoding, and the strings in the memory and in the file are the
same, now I'm trying with other strings.

Thanks again.

Freddy Coal



AlexS said:
Freddy,

you can try this

myString = System.Text.Encoding.GetEncoding(28591).GetString(data)

where data is byte[]. You read your file in data with ReadAllBytes as you
do now

Alex

Freddy Coal said:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.

I make this function for make that:

'--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
dato = My.Computer.FileSystem.ReadAllBytes(ruta)
For Each dat As Byte In dato
cadena = cadena & Microsoft.VisualBasic.ChrW(dat)
Next
Return cadena
Else
Return "Error"
End If
End Function
'--------------------------

Well, my problem is that in the final string, some bytes in the string
not are the originals, How can correct that?.

Thanks in advance for any help.

Freddy Coal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top