Advertisement
YasserKhalil2019

T3778_Save Restore Files In Hex

Aug 23rd, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. https://excel-egy.com/forum/t3778
  2. ---------------------------------
  3.  
  4. Sub Save_Hex_File()
  5. Dim wks As Worksheet
  6. Dim fileName As String
  7.  
  8. Set wks = Worksheets("Hex Byte Data")
  9. fileName = ThisWorkbook.Path & "\" & wks.Range("AH1").Value
  10.  
  11. If Dir(fileName) <> "" Then
  12. Call SaveAsHexFile(fileName)
  13. MsgBox "File Saved", 64
  14. Else
  15. MsgBox "The File '" & fileName & "' Not Found."
  16. End If
  17. End Sub
  18.  
  19. Sub Restore_Hex_File()
  20. Dim wks As Worksheet
  21. Dim fileName As String
  22.  
  23. Set wks = Worksheets("Hex Byte Data")
  24. fileName = ThisWorkbook.Path & "\" & wks.Range("AH1").Value
  25.  
  26. fileName = RestoreHexFile
  27. MsgBox "File Restored", 64
  28. End Sub
  29.  
  30. Private Sub SaveAsHexFile(ByVal fileName As String)
  31. Dim wks As Worksheet
  32. Dim data() As Variant
  33. Dim x As String
  34. Dim dataByte As Byte
  35. Dim n As Integer
  36. Dim i As Long
  37. Dim r As Long
  38. Dim c As Long
  39.  
  40. If Dir(fileName) = "" Then Exit Sub
  41.  
  42. On Error Resume Next
  43. Set wks = Worksheets("Hex Byte Data")
  44. If Err = 9 Then
  45. Worksheets.Add After:=Worksheets.Count
  46. Set wks = ActiveSheet
  47. wks.Name = "Hex Byte Data"
  48. End If
  49. On Error GoTo 0
  50.  
  51. wks.Cells.ClearContents
  52. wks.Cells(1, "AH").Value = Dir(fileName)
  53.  
  54. n = FreeFile
  55.  
  56. Application.ScreenUpdating = False
  57. Application.ErrorCheckingOptions.NumberAsText = False
  58.  
  59. With wks.Columns("A:AF")
  60. .NumberFormat = "@"
  61. .Cells.HorizontalAlignment = xlCenter
  62.  
  63. Open fileName For Binary Access Read As #n
  64. ReDim data((LOF(n) - 1) \ 32, 31)
  65.  
  66. For i = 0 To LOF(n) - 1
  67. Get #n, , dataByte
  68. c = i Mod 32
  69. r = i \ 32
  70. x = Hex(dataByte)
  71. If dataByte < 16 Then x = "0" & x
  72. data(r, c) = x
  73. Next i
  74.  
  75. Close #n
  76.  
  77. wks.Range("A1:AF1").Resize(r + 1, 32).Value = data
  78. End With
  79. Application.ScreenUpdating = True
  80. End Sub
  81.  
  82. Function RestoreHexFile() As String
  83. Dim wks As Worksheet
  84. Dim lsb As Variant
  85. Dim msb As Variant
  86. Dim cell As Range
  87. Dim rng As Range
  88. Dim file As String
  89. Dim n As Integer
  90. Dim data() As Byte
  91. Dim j As Long
  92.  
  93. On Error Resume Next
  94. Set wks = Worksheets("Hex Byte Data")
  95. If Err <> 0 Then
  96. MsgBox "The Worksheet 'Hex Byte Data' Is Missing.", vbCritical
  97. Exit Function
  98. End If
  99. On Error GoTo 0
  100.  
  101. Set rng = wks.Range("A1").CurrentRegion
  102. file = wks.Cells(1, "AH").Value
  103.  
  104. If file <> "" Then
  105. n = FreeFile
  106. file = ThisWorkbook.Path & "\" & file
  107.  
  108. Open file For Binary Access Write As #n
  109. ReDim data(Application.CountA(rng) - 1)
  110.  
  111. For Each cell In rng
  112. If cell = "" Then Exit For
  113.  
  114. msb = Left(cell, 1)
  115. If IsNumeric(msb) Then msb = 16 * msb Else msb = 16 * (Asc(msb) - 55)
  116.  
  117. lsb = Right(cell, 1)
  118. If Not IsNumeric(lsb) Then lsb = (Asc(lsb) - 55) Else lsb = lsb * 1
  119.  
  120. data(j) = msb + lsb
  121. j = j + 1
  122. Next cell
  123.  
  124. Put #n, , data
  125. Close #n
  126. End If
  127.  
  128. RestoreHexFile = file
  129. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement