Read file per 4 chars

Ontani

Well-known member
Joined
Mar 3, 2004
Messages
119
Location
Belgium
i need to read a text file per 4 chars

the file is coded with a simpel Xor.

file:

127712771266120012071185119012031212

it should be something like this:

[VB]
decoded(0) = 1277
decoded(1) = 1277
decoded(2) = 1266
decoded(3) = 1200
decoded(4) = 1207
...
[/VB]

then they have to be decoded so:
[VB]
Dim password as Integer = 1234
decoded(0) = chr(decoded(0) Xor password) decoded(0) = "/"
decoded(1) = chr(decoded(1) Xor password) decoded(1) = "/"
decoded(2) = chr(decoded(2) Xor password) decoded(2) = " "
decoded(3) = chr(decoded(3) Xor password) decoded(3) = "b"
decoded(4) = chr(decoded(4) Xor password) decoded(4) = "e"
...
[/VB]


This is my code at the moment:

[VB]
Dim password as Integer = 1234
Dim file As String = OpenFileDialog1.FileName
Dim objLeesBestand As New System.IO.StreamReader(file)
Dim i, a As Integer
Dim lengteBestand As Long = objLeesBestand.BaseStream.Length
For i = 0 To i = (lengteBestand / 4) Step 4
ReDim Preserve strBlok(i + 4)
objLeesBestand.Read(strBlok, i, 4)
ReDim Preserve decoded(a)
decoded(a) = strBlok(i) & strBlok(i + 1) & strBlok(i + 2) & strBlok(i + 3)
decoded(a) = decoded(a) Xor password
TextBox2.Text &= Chr(decoded(a))
a += 1
Next
objLeesBestand.Close()
objLeesBestand = Nothing
[/VB]

The problem is:
Textbox2.Text contains only the first decoded char and the arrays only have 1 value.
For some reason this For Next isnt completed.

Does Anybody has an idea what went wrong?
 
Last edited by a moderator:
A couple of things Im wondering:
For i = 0 To i = (lengteBestand / 4) Step 4
There doesnt need to be a second i = in this For Loop line
For i = 0 To (lengteBestand / 4) Step 4
It looks suspicious that you are dividing the length by 4 AND stepping by four. This would be roughly equivalent to division by 16.
Instead, it seems like you shouldnt need to divide:
For i = 0 To lengteBestand Step 4

What is strBlok declared as? Looks like it is declared as a string.
If so, what is decoded declared as? Looks like it is declared as an integer.
And, Option Strict will not allow you to do this line:
decoded(a) = strBlok(i) & strBlok(i + 1) & strBlok(i + 2) & strBlok(i + 3)
Instead, you would convert it yourself.
decoded(a) = Integer.Parse(strBlok(i) & strBlok(i + 1) & strBlok(i + 2) & strBlok(i + 3))
When adding the decoded value to the textbox:
TextBox2.Text &= Chr(decoded(a))
You should use .AppendText instead of the &= operator.
TextBox2.AppendText(Chr(decoded(a))
And also, go for Convert.ToChar() instead of the old Chr() function
TextBox2.AppendText(Convert.ToChar(decoded(a))
Also, you seem to be doing a lot of Redim Preserves (which you should avoid in .NET anyway), but you should only be doing one, especially if it is in a For Loop.
ReDim Preserve strBlok(i + 4)
You should move this out of the For loop and redim it so that you already have the required length for it
ReDim Preserve strBlok(lengteBestand)
Same with
ReDim Preserve decoded(a)
Which can be moved out of the for loop and redimmed with
ReDim Preserve decoded(lengteBestand \ 4)

With Option Strict on, you wouldve easily caught that For Loop error.
For i = 0 To i = (lengteBestand / 4) Step 4
what happens is the second
i = (lengteBestand / 4)
turns into a boolean check, not an assignment.
Of course, i is not equal to (lengteBestand / 4)... which by the way, would be faster as (lengteBestand \ 4), where the backslash is for integer division.
Since i is not equal to lengteBestand / 4, this returns False, which is equivalent to the Integer value of 0.
Your For loop is now
For i = 0 To 0 Step 4
which is why the For Loop only runs once. :) Option Strict will have caught the unwanted conversion from Boolean to Integer for you.
 
thanx got it to work,

need to learn to work with this Option Explicit On thingy.
works real nice and prevents 50% off my mistakes.
 
Back
Top