![]() ![]() |
Oct 27 2009, 10:31 AM
Post
#1
|
|||
|
Scourge for president!
|
For my utility I planed to make a Level select text editor. Loading files and editing them works just fine. There is only one thing left: Saving the shit. Because I did the code for loading the file so long ago I totally forgot what it's doing, meaning I can't change it to the opposite. If there's anyone around here who can work with Hex in VB, please look at this and tell me what you would do:
CODE Dim LoadedFile As String = OpenFileDialog1.FileName
Dim fs As New System.IO.FileStream(LoadedFile, IO.FileMode.Open) Dim br As New System.IO.BinaryReader(fs) Dim bytes() As System.Byte Dim laenge As Integer laenge = fs.Length bytes = br.ReadBytes(fs.Length) br.Close() fs.Close() For Each b As System.Byte In bytes Me.RTBHex.Text += System.String.Format("{0:X2} ", b) Next |
||
Oct 27 2009, 10:49 AM
Post
#2
|
|||
|
No way! I can't believe this!
|
Save yourself aggravation and use
My.Computer.FileSystem.ReadAllBytes(file as String) as Byte() and My.Computer.FileSystem.WriteAllBytes(file as String, data as Byte(), append as Boolean) Also, you should use the & Operator for String concatenation, to avoid confusion when working with strings and numbers. This post has been edited by MainMemory: Oct 27 2009, 10:55 AM |
||
Oct 27 2009, 11:25 AM
Post
#3
|
|||
|
Scourge for president!
|
I think this will help me. But I don't get how to show the loaded in a Textbox, aka. how to convert it to a string.
|
||
Oct 27 2009, 11:33 AM
Post
#4
|
|||
|
No way! I can't believe this!
|
|||
Oct 27 2009, 11:41 AM
Post
#5
|
|||
|
Scourge for president!
|
CODE For Each b As System.Byte In bytes Me.RTBHex.Text += System.String.Format("{0:X2} ", b) Next What exactly was this code doing? What I wanted (lol). It reads every byte and changes it into a string (and putting a space for a better view). This made my code much shorter now (thanks for this one). Actually I just need to change this code to the opposite now, but here's the problem again, into what? |
||
Oct 27 2009, 11:49 AM
Post
#6
|
|||
|
No way! I can't believe this!
|
I think it should look something like this:
CODE Dim bytes((RTBHex.Length - 1) \ 2) as Byte For b As Integer = 0 To RTBHex.Length - 1 Step 2 bytes(b \ 2) = "&H" & RTBHex.Substring(b, 2) Next My.Computer.FileSystem.WriteAllBytes(filename, bytes, False) But I'm not entirely sure on that. Edit: Normally, I keep everything in the byte array and read from and write to that when I need something from it. Edit2: Some examples: CODE Dim MyString as String = System.Text.Encoding.ASCII.GetString(bytes, address, length) Array.Clear(bytes, address, maxlength) 'Clear the array in case the string was shortened. System.Text.Encoding.ASCII.GetBytes(MyString).CopyTo(bytes, address) ---- Dim ByteValue as Byte = bytes(address) bytes(address) = ByteValue ---- Dim TwoByteValue as UShort = BitConverter.ToUInt16(bytes, address) BitConverter.GetBytes(TwoByteValue).CopyTo(bytes, address) ---- Dim FourByteValue as UInteger = BitConverter.ToInt32(bytes, address) BitConverter.GetBytes(FourByteValue).CopyTo(bytes, address) ---- Dim SignedTwoByteValue as Short = BitConverter.ToInt16(bytes, address) BitConverter.GetBytes(SignedTwoByteValue).CopyTo(bytes, address) ---- Dim FloatValue as Single = BitConverter.ToSingle(bytes, address) BitConverter.GetBytes(FloatValue).CopyTo(bytes, address) Note that you may have to use one of the conversion functions if it's not the right type, or BitConverter.GetBytes() will give you the wrong values! Also note that the Genesis uses a different byte order than the PC, so the two+ byte ones won't work there unless you reverse the order. This post has been edited by MainMemory: Oct 27 2009, 12:23 PM |
||
Oct 27 2009, 12:22 PM
Post
#7
|
|||
|
I am the Holy Cat
|
*shakes head*
I wonder why they had to make things this complicated in newer versions of VB. I'll never trade my VB5 with any of them. |
||
Oct 27 2009, 12:23 PM
Post
#8
|
|||
|
No way! I can't believe this!
|
And how do you read from binary files in VB5?
|
||
Oct 27 2009, 12:35 PM
Post
#9
|
|||
|
I am the Holy Cat
|
Although there are various methods, my preferred one is this, which interestingly enough is a legacy method from QB, even.
CODE open "whatever.bin" for binary as #1 I now have a string a$ which can be used in any way I want. By using asc(a$) I can get the ascii value in decimal; with hex$(asc(a$)) I can get the hex value; and so on. If I needed a longer string, I could have just raised the number of characters in the space$() statement. This is as simple as it gets. And to write to a file, the same method applies. You make up your string in whatever way you want, possibly using either the chr$() or the str$() command according to what you have, and then you write it back with a put #1.a$=space$(1) get #1,,a$ Close #1 No thanks, this can be obsolete as you want, but no way I'm going to switch to the new awkward syntax. Note that I even used the unnecessary $ suffix instead of declaring them as strings, this is as oldschool as it gets, I started on a commodore 16 and I'm proud of that! |
||
Oct 27 2009, 12:57 PM
Post
#10
|
|||
|
No way! I can't believe this!
|
Good for you, I guess I just prefer one line to all that.
Dim z as Byte() = My.Computer.FileSystem.ReadAllBytes("lol.bin") Opens, reads, and closes the file. My.Computer.FileSystem.WriteAllBytes("lol2.bin", z, False) Opens, writes, and closes the file. I started programming on a TI-83+. :D |
||
Oct 27 2009, 01:16 PM
Post
#11
|
|||
|
Merry Christmas
|
Ugh. That syntax is so awful.
That's the reason I use VB6. |
||
Oct 27 2009, 01:32 PM
Post
#12
|
|||
|
Scourge for president!
|
Right, now anything works. Looking at the last change of the file, it has been edited with the program. However, it's (of course) saving the things I've been using for the loading of the file, so there's no change. Now I need to find out how to convert the string to hex again and I'm actually done, and obviously I don't know how to do that.
@Oerg: I'm still planning to switch to VB6, though I don't think it's possible to convert VB 2008 to VB6. :/ This post has been edited by Selbi: Oct 27 2009, 01:35 PM |
||
Oct 27 2009, 01:35 PM
Post
#13
|
|||
|
No way! I can't believe this!
|
So, what does the string look like? A combination of hex values like "FE6D22A8"? I can't help you unless I know what I'm converting.
|
||
Oct 27 2009, 01:40 PM
Post
#14
|
|||
|
Scourge for president!
|
Because I suck at explaining, I'll just show you a picture of the tool:
![]() The text out of the second Textbox is the string I'm talking about. |
||
Oct 27 2009, 01:54 PM
Post
#15
|
|||
|
No way! I can't believe this!
|
Assuming that the text is separated by spaces without any newlines and with no extra space at the end, you should be able to do this:
CODE Dim HexText as String() = TextBox2.Text.Split(" ") 'Split the text by spaces
Dim bytes(HexText.Length - 1) as Byte 'give the output array the same number of elements as the text For index as Integer = 0 to HexText.Length - 1 bytes(index) = "&H" & HexText(index) 'convert to byte Next |
||
![]() ![]() |
|
Lo-Fi Version | Time is now: 21st November 2009 - 02:11 PM |