473,287 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Reading Binary file, the code change bytes

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.
Apr 2 '07 #1
11 3544
Likely a character encoding issue. You assume that each byte encode a single
character which is not necessaryly true. What this "binary" file is supposed
to be ?

If this is a "true" binary file it should be likely processed as an array of
byte. If this is text, it should be likely read with the proper character
encoding.

See around
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx

Patrice

"Freddy Coal" <fr********@gmail.withoutspan.coma écrit dans le message de
news: On****************@TK2MSFTNGP04.phx.gbl...
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.

Apr 2 '07 #2
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/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(VS.80).aspx

"Patrice" <http://www.chez.com/scribe/wrote in message
news:uM**************@TK2MSFTNGP05.phx.gbl...
Likely a character encoding issue. You assume that each byte encode a
single character which is not necessaryly true. What this "binary" file is
supposed to be ?

If this is a "true" binary file it should be likely processed as an array
of byte. If this is text, it should be likely read with the proper
character encoding.

See around
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx

Patrice

"Freddy Coal" <fr********@gmail.withoutspan.coma écrit dans le message
de news: On****************@TK2MSFTNGP04.phx.gbl...
>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.


Apr 2 '07 #3
Freddy Coal wrote:
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/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 2 '07 #4
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

"Göran Andersson" <gu***@guffa.comwrote in message
news:eT**************@TK2MSFTNGP06.phx.gbl...
Freddy Coal wrote:
>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/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(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.

--
Göran Andersson
_____
http://www.guffa.com

Apr 3 '07 #5
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" <fr********@gmail.withoutspan.coma écrit dans le message de
news: e8*************@TK2MSFTNGP06.phx.gbl...
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

"Göran Andersson" <gu***@guffa.comwrote in message
news:eT**************@TK2MSFTNGP06.phx.gbl...
>Freddy Coal wrote:
>>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/lib...8t(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...28(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.

--
Göran Andersson
_____
http://www.guffa.com


Apr 3 '07 #6
Freddy Coal wrote:
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

--
Göran Andersson
_____
http://www.guffa.com
Apr 3 '07 #7
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.
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.

Apr 4 '07 #8
Freddy Coal wrote:
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[i] = 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.

--
Göran Andersson
_____
http://www.guffa.com
Apr 4 '07 #9
Göran Andersson wrote:
Freddy Coal wrote:
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.
<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.

Apr 5 '07 #10
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" <fr********@gmail.withoutspan.comwrote in message
news:On****************@TK2MSFTNGP04.phx.gbl...
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.


Apr 5 '07 #11
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" <sa***********@SPAMrogers.comPLEASEwrote in message
news:uJ*************@TK2MSFTNGP02.phx.gbl...
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" <fr********@gmail.withoutspan.comwrote in message
news:On****************@TK2MSFTNGP04.phx.gbl...
>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.



Apr 9 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.