cerco una macro che mi permetta di importare un file hex in excel.
tanks
I want a macro that allows me to import a hex file into Excel.
tanks
cerco una macro che mi permetta di importare un file hex in excel.
tanks
I want a macro that allows me to import a hex file into Excel.
tanks
Last edited by tatop; 01-19-2012 at 07:09 PM. Reason: error only italian
a hex file? what is that. is it a text file with hex numbers?
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and now happily retired
where does code go ?
look here
how to insert code
how to enter array formula
why use -- in sumproduct
recommended reading
wiki Mojito
how to say no convincingly
most important thing you need
Martin Wilson: SPV
and RSMBC
If you have a bin(ary) file, then don't you want to open it in a hex editor to review/make changes? Apologies, i just can't see why you might open it in Excel...
Not too complicated; reading as HEX file, converting to ascii
![]()
sub snb() Open "E:\file.bin" For Binary As #1 c00 = Input(LOF(1), #1) Close #1 redim sn(len(c00)+1) For j = 1 To len(c00) sn(j) = Asc(Mid(c00, j, 1)) Next cells(1)=join(sn,"") End sub
i still don't understand, what would an example excel file look like?
0 1 2 3 4 5 6 7 8 9 a b c d e f
0 af 12 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac
10 5f 6f ff ff 12 ff 44 5f 6f ff 5f 6f ff ff 12 12
20 f1 34 f1 34 55 89 ff ac ff 44 5f 6f ff ff 12 12
30 6f f1 34 55 89 f1 34 55 89 ff ac ff 44 5f 6f ff
40 5f 6f ff ff 12 5f 6f ff ff 12 ff 44 5f 6f ff ff
50 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac ff ff
60 f1 34 55 89 5f 6f ff ff 12 12 f1 34 55 89 ff ac
70 f1 34 55 89 5f 6f ff ff 12 12 f1 34 55 89 ff ac
80 f1 34 55 89 ff ac ff 44 5f 6f ff ff 12 ff 44 5f
90 ff 44 5f 6f ff bf ff 44 5f 55 89 ff ac ff 44 5f
100 f1 34 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac
Yes I see, but are you aware that a cell in Excel can only show a limited amount of characters ?
So where do you want the hex characters to be shown in Excel ??
You only have to change the file's name
![]()
Sub snb() Open "G:\midbmut_snb.dbf" For Binary As #1 c00 = Input(LOF(1), #1) Close #1 For j = 1 To Len(c00) Cells(j \ 16 + 1, (j - 1) Mod 16 + 1) = "'" & Right("00" & Hex(Asc(Mid(c00, j, 1))), 2) Next End Sub
thanks for the macro, but I have two problems:
1) the last cell of the first line is empty and the value of that cell is inserted into the cell below, so moving 'the last column at the bottom of a cell;
2) values in individual cells are displayed as text but I would display them as hexadecimal values as I have to make calculations.
You can edit the macro please
thanks a lot
Try this one:
![]()
Option Explicit Sub BinToHex() Dim ColumnNumber As Long, _ RowNumber As Long, _ Ndx As Long, _ BEN As Worksheet, _ InBucket As Variant Set BEN = Sheets("BEN") Open "c:\downloads\encoding.bin" For Binary As #1 InBucket = Input(LOF(1), #1) Close #1 RowNumber = 1 ColumnNumber = 0 For Ndx = 1 To Len(InBucket) ColumnNumber = ColumnNumber + 1 BEN.Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1))) If (Ndx Mod 16) = 0 Then RowNumber = RowNumber + 1 ColumnNumber = 0 End If Next Ndx End Sub
Ben Van Johnson
tanks but when play macro :
index out of range
When I was writing/testing the macro, I used a file from my own computer and entered the output to a worksheet I named BEN. The mod below will write the output to what ever the active sheet is. But you must still tell the macro where the binary file is located >> change the BLUE portion of the code below.
1. What is the path and name of your file ?![]()
Option Explicit Sub BinToHex() Dim ColumnNumber As Long, _ RowNumber As Long, _ Ndx As Long, _ InBucket As Variant Open "c:\downloads\encoding.bin" For Binary As #1 InBucket = Input(LOF(1), #1) Close #1 RowNumber = 1 ColumnNumber = 0 For Ndx = 1 To Len(InBucket) ColumnNumber = ColumnNumber + 1 Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1))) If (Ndx Mod 16) = 0 Then RowNumber = RowNumber + 1 ColumnNumber = 0 End If Next Ndx End Sub
2. Where did you copy the code (did you put it in a worksheet module)?
TANKS, works perfectly ....... but I still need you !!!!!!
I'm sorry .... Once I changed the file in excel how do I save it in. BIN?? I need to calculate a few bytes to join 4-cell, and I do it with the following function: "= A1&A2&A3&A4 "..... but then how do I rewrite the result obtained on 4 separate cells?hopefully last question: can you give me create a macro that I want to ask me to upload files without having to change each time the macro??
tanks
tanks
Does the EEPROM use Big- or Little-endian data? That may well determine how you want the data extracted and written to the file.
Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.
Thanks.
Entia non sunt multiplicanda sine necessitate
tatop seems to be done but I thought I would post the update anyway:![]()
Option Explicit Option Base 1 Sub BinToHex() Dim ColumnNumber As Long, _ RowNumber As Long, _ Ndx As Long, _ handleNumber As Long, _ InBucket As Variant, _ FileToOpen As String 'get the name of the binary file to open FileToOpen = Application.GetOpenFilename("Binary files (*.bin),*.bin") ' get the next handle number from windows handleNumber = FreeFile 'open the file, read into a string Open FileToOpen For Binary As handleNumber InBucket = Input(LOF(handleNumber), handleNumber) Close handleNumber RowNumber = 1 ColumnNumber = 0 ' for each byte of the string, convert to decimal integer value 0 - 256, ' convert the integer to hex For Ndx = 1 To Len(InBucket) ColumnNumber = ColumnNumber + 1 Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1))) If (Ndx Mod 16) = 0 Then RowNumber = RowNumber + 1 ColumnNumber = 0 End If Next Ndx End Sub
![]()
Sub SaveAsBinary() Dim LastRow As Long, _ RowNdx As Long, _ ColNdx As Long, _ FileNum As Long, _ t0 As Double, _ FName FName = Application.GetSaveAsFilename t0 = Timer LastRow = Cells(Rows.Count, "R").End(xlUp).Row ReDim outarray(1 To LastRow, 1 To 16) For RowNdx = 1 To LastRow For ColNdx = 1 To 16 ' convert the cell hex back to decimal and replace with the character string outarray(RowNdx, ColNdx) = Chr(WorksheetFunction.Hex2Dec(Cells(RowNdx, ColNdx).Value)) Next ColNdx Next RowNdx 'open the file and save the array FileNum = FreeFile Open FName For Binary As FileNum Put FileNum, , outarray Close FileNum Debug.Print "elapsed time ", Timer - t0 End Sub
up ! ! ! ! !
Sorry, the save macro runs on my machine. I'm not familiar with runtime error 438. Can you step through the "save" macro to see which line is highlighted when it stops?
error 438.jpg
error of runtime "438"
Property or method not supported
Attachment 137838
error of runtime "438"
Property or method not supported
I'm sorry, the macro in Excel 2007 works, the problem was that I was using Excel 2003. I have it converted and it works .... only problem is that, hex conversion is incorrect, because when I save the bin file I see only data wrong.
my new code is:
![]()
Sub SaveAsBinary() Dim LastRow As Long, _ RowNdx As Long, _ ColNdx As Long, _ FileNum As Long, _ t0 As Double, _ FName FName = Application.GetSaveAsFilename t0 = Timer LastRow = Cells(Rows.Count, "R").End(xlUp).Row ReDim outarray(1 To LastRow, 1 To 16) For RowNdx = 1 To LastRow For ColNdx = 1 To 16 ' convert the cell hex back to decimal and replace with the character string outarray(RowNdx, ColNdx) = Evaluate("Chr(Clng(RowNdx, ColNdx).Value)))") Next ColNdx Next RowNdx 'open the file and save the array FileNum = FreeFile Open FName For Binary As FileNum Put FileNum, , outarray Close FileNum Debug.Print "elapsed time ", Timer - t0 End Sub
O.k. the error 438 is because you don't seem to have the Analysis ToolPak installed on your machine:
2. In the line:HEX2DEC
If this function is not available, and returns the #NAME? error, install and load the Analysis ToolPak add-in.
On the Tools menu, click Add-Ins.
In the Add-Ins available list, select the Analysis ToolPak box, and then click OK.
If necessary, follow the instructions in the setup program.
you are trying to use the CLng function to convert a value on the worksheet to a long integer. However, you have made a couple of mistakes.![]()
outarray(RowNdx, ColNdx) = Evaluate("Chr(Clng(RowNdx, ColNdx).Value)))")
The Clng function works with decimal values such as 1202.667 converting it to 1203. In your spreadsheet you have strings representing hex numbers such as "FE" in a cell, say D4.
Since F & E are not decimal digits, were you to write the function correctly you would write Clng(Range("D4").Value) or Clng(Cells(RowNdx,ColNdx).Value) you would get an error.
But, you wrote the Clng function wrong, so it returns an error value to the Chr() function and it in turn errors since there is no character mapped to those error values.
The result is that the line simply loads the output array with error values.
-------------------------
Also, the lines:
t0 = Timer, and
Debug.Print "elapsed time ", Timer - t0 should be deleted, that was just an experiment for me while making changes.![]()
Last edited by protonLeah; 01-25-2012 at 12:09 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks