You are not logged in.

Juanfelipe

Beginner

  • "Juanfelipe" is male
  • "Juanfelipe" started this thread

Posts: 2

Date of registration: Mar 29th 2009

Location: Brazil

Occupation: VSP

  • Send private message

1

Sunday, March 29th 2009, 6:21pm

Manipulate binary files?

HI!
I am not expert in VB, but I do some things "interesting". ^^
I did the following functions to read and write bytes from a file:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Public Function Read_Hex(file_name As String, offset As Long, size As Integer) As String
On Error GoTo erro
Dim ROM As Long
Dim i As Integer
Dim data As Byte
Dim data1 As String
ROM = FreeFile
Open file_name For Binary As ROM
For i = 1 To size
    Get ROM, offset + i, data
    data1 = data1 & Right$("00" & Hex(data), 2)
Next
Read_Hex = Right("000000000000" & data1, size * 2)
Close ROM
Exit Function

erro:
MsgBox Err.Description, vbOKOnly, "Error: " & Err.Number
End Function

Public Function WriteHex(file_name As String, offset As Integer, size As Integer, data As String) As Boolean
On Error GoTo erro
Dim ROM As Long
Dim data1 As String
Dim data2 As Byte
ROM = FreeFile
Open file_name For Binary As ROM
offset = offset + 1
'data1 = Hex(data)
Do While (Len(data1) Mod 2 <> 0)
    data = "0" & data
Loop
'Do While Len(data) > 0
Do While data <> ""
    data2 = Val("&H" & Mid(data, 1, 2))
    Put ROM, offset, data2
    data = Right$(data1, Len(data) - 2)
    offset = offset + 1
Loop
Close ROM
Exit Function

erro:
MsgBox Err.Description, vbOKOnly, "Error: " & Err.Number
End Function


In Jabaco, gives error in lines that are in bold.
These lines would be to:
"Release" the file : ROM = FreeFile
Open the file : Open file_name For Binary As ROM
Read a byte of the file : Get ROM, offset + i, data
Write a byte in the file : Put ROM, offset, data2
Close the file : Close ROM

Someone could tell me how to use these commands in Jabaco?


Excuse me for my English but I am Brazilian, and not learned English in school. :rolleyes:

EDIT:
How can I convert a hexadecimal(string)to decimal(integer)?
In VB6 I use:

Source code

1
num_dec = val("&H" & right("00" & num_hex,2))

But don't work :/

This post has been edited 1 times, last edit by "Juanfelipe" (Mar 29th 2009, 6:27pm)


OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Monday, March 30th 2009, 12:37am

convert hexadecimal string to integer

Hi,

Quoted

How can I convert a hexadecimal(string)to decimal(integer)? But don't work :/


ah actually ... you could use the function instead: Integer.parseInt

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
Dim i As Integer
   Dim s As String
   
   i = 65
   
   s = "&H" & Hex$(i)
   
   MsgBox CStr(i) & " = " & s
   s = Replace(s, "&H", "")
   i = Integer.parseInt(s, 16)
   
   MsgBox CStr(i)


the second parameter of parseInt sets the base of the value in s: here it is 16

greetings

OlimilO

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Monday, March 30th 2009, 12:10pm

Put, Get

Quoted

Someone could tell me how to use these commands in Jabaco?


look here maybe this can help a little bit

Reading and writing binary data

greetings OlimilO

Juanfelipe

Beginner

  • "Juanfelipe" is male
  • "Juanfelipe" started this thread

Posts: 2

Date of registration: Mar 29th 2009

Location: Brazil

Occupation: VSP

  • Send private message

4

Monday, March 30th 2009, 2:59pm

Thank you!
I already managed to work with binary files.
Just needed to know how to convert hexadecimal to decimal.
Thank you very much!
:thumbsup:


Rate this thread
WoltLab Burning Board