Welcome Guest ( Log In | Register )


Help | Search | Members | Calendar
IRC Chat: irc.badnik.net | CGI:IRC
IPB
home | info | forums | svn | irc | podcast | about
2 Pages V   1 2 >  
Reply to this topicStart new topic
> Saving in hex using VB
 Selbi
post Oct 27 2009, 10:31 AM
Post #1


Scourge for president!

Group Icon

Group: Members
Posts: 215
Edits: 172
Joined: 12-May 08
From: Germany

Current Project:
S.C.H.ERZ and S1TCG



Hacking Utility ProgrammerWiki Restoration Contributor

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
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 10:49 AM
Post #2


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

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
Go to the top of the page
+Quote Post
 Selbi
post Oct 27 2009, 11:25 AM
Post #3


Scourge for president!

Group Icon

Group: Members
Posts: 215
Edits: 172
Joined: 12-May 08
From: Germany

Current Project:
S.C.H.ERZ and S1TCG



Hacking Utility ProgrammerWiki Restoration Contributor

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.
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 11:33 AM
Post #4


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

If you want to convert a Byte to a String in decimal, use CStr(byte). If you want a hex string, use Hex(byte).

QUOTE(Selbi @ Oct 27 2009, 10:31 AM) *
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?
Go to the top of the page
+Quote Post
 Selbi
post Oct 27 2009, 11:41 AM
Post #5


Scourge for president!

Group Icon

Group: Members
Posts: 215
Edits: 172
Joined: 12-May 08
From: Germany

Current Project:
S.C.H.ERZ and S1TCG



Hacking Utility ProgrammerWiki Restoration Contributor

QUOTE(MainMemory @ Oct 27 2009, 05:33 PM) *
QUOTE(Selbi @ Oct 27 2009, 10:31 AM) *
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?
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 11:49 AM
Post #6


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

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
Go to the top of the page
+Quote Post
 nineko
post Oct 27 2009, 12:22 PM
Post #7


I am the Holy Cat

Group Icon

Group: Wiki Sysops
Posts: 3,129
Edits: 3,276
Joined: 17-August 06
From: italy

Current Project:
trying to become a better person



Hacking Utility ProgrammerBest Member EverHidden Palace Donator6th Annual Hacking Contest - Trophy WinnerWiki Restoration Contributor

*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.
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 12:23 PM
Post #8


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

And how do you read from binary files in VB5?
Go to the top of the page
+Quote Post
 nineko
post Oct 27 2009, 12:35 PM
Post #9


I am the Holy Cat

Group Icon

Group: Wiki Sysops
Posts: 3,129
Edits: 3,276
Joined: 17-August 06
From: italy

Current Project:
trying to become a better person



Hacking Utility ProgrammerBest Member EverHidden Palace Donator6th Annual Hacking Contest - Trophy WinnerWiki Restoration Contributor

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
a$=space$(1)
get #1,,a$
Close #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.

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!
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 12:57 PM
Post #10


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

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
Go to the top of the page
+Quote Post
 Oerg866
post Oct 27 2009, 01:16 PM
Post #11


Merry Christmas

Group Icon

Group: Members
Posts: 948
Edits: 212
Joined: 7-September 06
From: frankfurt/germany

Current Project:
Sonic 1 Oergomized



Hacking Utility ProgrammerWiki Restoration Contributor

Ugh. That syntax is so awful.

That's the reason I use VB6.
Go to the top of the page
+Quote Post
 Selbi
post Oct 27 2009, 01:32 PM
Post #12


Scourge for president!

Group Icon

Group: Members
Posts: 215
Edits: 172
Joined: 12-May 08
From: Germany

Current Project:
S.C.H.ERZ and S1TCG



Hacking Utility ProgrammerWiki Restoration Contributor

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
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 01:35 PM
Post #13


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

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.
Go to the top of the page
+Quote Post
 Selbi
post Oct 27 2009, 01:40 PM
Post #14


Scourge for president!

Group Icon

Group: Members
Posts: 215
Edits: 172
Joined: 12-May 08
From: Germany

Current Project:
S.C.H.ERZ and S1TCG



Hacking Utility ProgrammerWiki Restoration Contributor

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.
Go to the top of the page
+Quote Post
 MainMemory
post Oct 27 2009, 01:54 PM
Post #15


No way! I can't believe this!

Group Icon

Group: Tech Members
Posts: 844
Edits: 490
Joined: 14-August 09

Current Project:
Disassembly of SADXPC.



Hacking Utility Programmer

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
Go to the top of the page
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 21st November 2009 - 02:11 PM
Bridged By IpbWiki: Integration Of Invision Power Board and MediaWiki © GlobalSoft