-
Jun 22nd, 2018, 09:42 AM
#1
Thread Starter
Lively Member
Concatinating bytes to bin file not being saved to output file
Hi, I am having a strange problem. I open a bin file, run and concatenate a word for a checksum in firmware.
Code below seems pretty straight forward:
1) open bin file - read all bytes
2) convert to integer
3) calculate the check sum to be added on (ignore the POLY portion)
4) add poly and checksum to byte array
5) Save byte file
The problem is, whenever I save the bin file, I get the same number of bytes whether the following portion of code is included or not (note this is the highlight of this post, and what is not working) -
Code:
Dim dummy(7) As Byte
array.Resize(byteRead, byteRead.Length + dummy.Length)
Dim temp_array() As Byte = BitConverter.GetBytes(integerStore(count - 1))
temp_array.CopyTo(byteRead, startIndex)
Dim temp_array2() As Byte = BitConverter.GetBytes(integerStore(count))
temp_array2.CopyTo(byteRead, startIndex + 4)
So my adding the bytes to the end of the byte array is not working properly. The strange thing is, if I put a break point right before saving the file, I do see the bytes in the byte array!? They just don't save???? Any clues?
Thanks!
Code:
Dim openFileDialog1 As New OpenFileDialog()
'Set settings for openfiledialog object
openFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
openFileDialog1.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
'Allow user to select file
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim filepath As String = System.IO.Path.GetFullPath(openFileDialog1.FileName)
Dim byteRead() As Byte
Dim POLY As UInt32 = &H4C11DB7&
byteRead = IO.File.ReadAllBytes(filepath)
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
'Set default file properties
With saveFileDialog1
.OverwritePrompt = True
.Filter = "bin File|*.bin"
.DefaultExt = ".bin"
' .AddExtension = True
' .InitialDirectory = "C:\"
End With
Dim integerStore(100000) As UInteger
Dim count As Integer = 0
Dim crc_check As UInteger = 0
For i As Integer = 0 To byteRead.Length - 1 Step 4
Dim one, two, three, four As UInteger
one = byteRead(i)
one = one << 24
two = byteRead(i + 1)
two = two << 16
three = byteRead(i + 2)
three = three << 8
four = byteRead(i + 3)
'four = four << 24
integerStore(count) = one Or two Or three Or four
count = count + 1
Next
integerStore(count) = POLY
Dim C_S As UInt32 = 0
For check_sum As Integer = 1 To count Step 1
C_S = C_S + integerStore(check_sum)
Next
Dim C_S_Final As UInt32 = 2 ^ 32 - C_S
Dim test As UInt32 = C_S_Final + C_S
count = count + 1
integerStore(count) = C_S_Final
Dim startIndex As Integer = byteRead.Length
Dim dummy(7) As Byte
array.Resize(byteRead, byteRead.Length + dummy.Length)
Dim temp_array() As Byte = BitConverter.GetBytes(integerStore(count - 1))
temp_array.CopyTo(byteRead, startIndex)
Dim temp_array2() As Byte = BitConverter.GetBytes(integerStore(count))
temp_array2.CopyTo(byteRead, startIndex + 4)
Dim binfile As String = saveFileDialog1.FileName.ToString & ".bin"
filepath = System.IO.Path.GetFullPath(saveFileDialog1.FileName)
IO.File.WriteAllBytes(filepath, byteRead)
End If
End If
Last edited by si_the_geek; Jun 22nd, 2018 at 09:54 AM.
Reason: added Code tags
-
Jun 22nd, 2018, 10:09 AM
#2
Re: Concatinating bytes to bin file not being saved to output file
Array.Resize is a function which returns the array.
You should probably assign it back to your variable.
byteRead = array.Resize(byteRead, byteRead.Length + dummy.Length)
-
Jun 22nd, 2018, 10:30 AM
#3
Thread Starter
Lively Member
Re: Concatinating bytes to bin file not being saved to output file
Hmmm...when I add that line of code, I get errors.
-
Jun 22nd, 2018, 10:59 AM
#4
Re: Concatinating bytes to bin file not being saved to output file
What errors? What's the error message?
My usual boring signature: Nothing
-
Jun 22nd, 2018, 11:06 AM
#5
Thread Starter
Lively Member
Re: Concatinating bytes to bin file not being saved to output file
Originally Posted by Shaggy Hiker
What errors? What's the error message?
Two things:
1) https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx - i do not see anything indicating that the array.resize is returning a value? And it doesn't make sense (to me) to assign a value to my array in that manner.
2) adding "byteRead = array.Resize(byteRead, byteRead.Length + dummy.Length)" causes build errors - "expression does not produce a value"
-
Jun 22nd, 2018, 11:29 AM
#6
Re: Concatinating bytes to bin file not being saved to output file
<Forget this post altogether, I need to wake up apparently>
I guess you're correct. I'll have to figure out what I was referencing.
I wonder if the Overwrite option has anything to do with it. If you're writing out the whole array, it doesn't seem like you would want to overwrite the old file, you would want to replace it with a bigger file, i.e. it should be just an Output mode. But that's talking off the top of my head, even more so than my previous post, which I though was correct. This one I'm not sure at all.
Last edited by passel; Jun 22nd, 2018 at 11:34 AM.
-
Jun 22nd, 2018, 11:36 AM
#7
Re: Concatinating bytes to bin file not being saved to output file
As a sanity check, did you check the modification date of the file? Is it actually being written to, just not the correct size, or is the file somehow being written to a different place or name?
For instance, assuming you have a safe copy of the file, when you read it in, change a few bytes at the front of the array, and then verify after you write it out whether you see those bytes being modified.
-
Jun 25th, 2018, 12:48 PM
#8
Thread Starter
Lively Member
Re: Concatinating bytes to bin file not being saved to output file
Thanks for the input passel. I definitely do write to a file, and save it as a new name. I can use the new file to program a device and it works. So everything is being saved, just not the last two bytes I concatenate, but I do see those bytes in the watch window of the file I am saving.
-
Jun 25th, 2018, 01:37 PM
#9
Re: Concatinating bytes to bin file not being saved to output file
I wrote this test. It reads all the bytes of a file to a List. It then concatenates some bytes to the end and writes it back to the file. Works like a charm. This is a text file so it converts 0 to 32 in the concat.
Code:
Private count As Integer = 32
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
count += 1
Dim concat() As Byte = BitConverter.GetBytes(count)
For idx As Integer = 0 To concat.Length - 1
If concat(idx) < 32 Then
concat(idx) = 32 'spaces
End If
Next
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
path = IO.Path.Combine(path, "foo.txt")
Dim byts As List(Of Byte) = IO.File.ReadAllBytes(path).ToList '<< to list
byts.AddRange(concat) '<< concat
IO.File.WriteAllBytes(path, byts.ToArray)
End Sub
The use of List makes things easier.
-
Jun 25th, 2018, 03:27 PM
#10
Re: Concatinating bytes to bin file not being saved to output file
p.s. I'm in a meeting so it took a long time to respond, so dbasnett got in while my focus was diverted, so you have two examples that don't have a problem. It still seems like somehow you may be interpreting your results incorrectly somehow. I don't know if your files are on a network, and there would be any flush/update time problem, but it seems unlikely.
I don't see what the problem is. It doesn't make any sense to me.
Also you are concatenating 8 bytes, but say "just not the last two bytes" are concatenated. You also say the file size doesn't change, which doesn't match with what you're saying.
Also, you say even if they write to a new file, the size is not correct.
I don't know how you can have these problems. It seems like you should be able to write a stand alone test to confirm the I/O method you're using.
For instance, I wrote a quick test, created an empty file in the directory of my choice, and had the code read the bytes in, add eight bytes to array read in, and write back out to the same file. Each time I read in, I print out the number of bytes read. I see the file increase by 8-bytes each time. If I look at the contents of the file I see the data I wrote to the file.
I can't see what your problem is.
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim filepath As String = System.IO.Path.GetFullPath("C:\c\test.bin")
Dim byteRead() As Byte
Dim POLY As UInt32 = &H4C11DB7&
Dim EndTag As UInt32 = &H4030201
byteRead = IO.File.ReadAllBytes(filepath)
Dim startIndex As Integer = byteRead.Count
Debug.Print("Bytes read = {0}", startIndex.ToString)
Array.Resize(byteRead, byteRead.Count + 8)
Dim temp_array() As Byte = BitConverter.GetBytes(POLY)
temp_array.CopyTo(byteRead, startIndex)
temp_array = BitConverter.GetBytes(EndTag)
temp_array.CopyTo(byteRead, startIndex + 4)
IO.File.WriteAllBytes(filepath, byteRead)
End Sub
End Class
Output in the Immediate window from the first run for 5 presses of the button.
Code:
Bytes read = 0
Bytes read = 8
Bytes read = 16
Bytes read = 24
Bytes read = 32
Last edited by passel; Jun 25th, 2018 at 03:32 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|