Board logo

標題: [發問] 從txt file讀出binary/hex data,結尾卻多出一個byte為0 [打印本頁]

作者: swatt    時間: 2020-8-26 17:59     標題: 從txt file讀出binary/hex data,結尾卻多出一個byte為0

各位大大們你好,請幫忙找一下問題:
我從hello.txt file 要讀出binary/hex data到excel cells,
但"hello"只有5個byte,可是用以下程式讀出來會再最後多出一個byte為0?
是程式寫法哪裡有誤嗎?

----------------------------------------------------------------
Sub Temp1()

Application.ScreenUpdating = False

    Dim intFileNum%, bytTemp As Byte, intCellRow%
    intFileNum = FreeFile
    intCellRow = 0

    fipath = "C:\hello.txt"
   
    Open fipath For Binary Access Read As intFileNum

    Do While Not EOF(intFileNum)
        intCellRow = intCellRow + 1
        Get intFileNum, , bytTemp
        Cells(1, intCellRow) = Hex(bytTemp) '' convert dec to hex

    Loop
    Close intFileNum
   
Application.ScreenUpdating = True

MsgBox "Done."

End Sub
-------------------------------------------------------------------------------------------------
[attach]32478[/attach]
作者: swatt    時間: 2020-8-27 11:00

回復 1# swatt

補充一下,此程式的目的是要以byte為長度,將任意檔案的原始碼(ASCII)寫到excel,
基本上程式是可行的,但就是在最後面會多出一個byte,這個byte一定是0,
找不出問題在哪裡,請大大們幫忙一下,感謝~
  1. Sub Temp1()

  2. Application.ScreenUpdating = False

  3.     Dim intFileNum%, bytTemp As Byte, intCellRow%
  4.     intFileNum = FreeFile
  5.     intCellRow = 0

  6.     fipath = "C:\hello.txt"
  7.    
  8.     Open fipath For Binary Access Read As intFileNum

  9.     Do While Not EOF(intFileNum)
  10.         intCellRow = intCellRow + 1
  11.         Get intFileNum, , bytTemp
  12.         Cells(1, intCellRow) = Hex(bytTemp) '' convert dec to hex

  13.     Loop
  14.     Close intFileNum
  15.    
  16. Application.ScreenUpdating = True

  17. MsgBox "Done."

  18. End Sub
複製代碼

作者: swatt    時間: 2020-8-27 17:36

本帖最後由 swatt 於 2020-8-27 17:40 編輯

回復 2# swatt

先繞路避過這個問題,不用EOF改用fil size 判斷檔案結束.
  1. Sub Temp1()

  2. Application.ScreenUpdating = False

  3.     Dim intFileNum%, bytTemp As Byte, intCellRow%
  4.     intFileNum = FreeFile
  5.     intCellRow = 1

  6.     fipath = "C:\hello.txt"
  7.    
  8.     flen = FileLen(fipath) 'get file length
  9.    
  10.     Open fipath For Binary Access Read As intFileNum

  11.     'Do While Not EOF(intFileNum) '用EOF判斷會在結尾多出一個byte為0
  12.     Do While intCellRow <= flen
  13.                 Get intFileNum, , bytTemp
  14.         Cells(1, intCellRow) = Hex(bytTemp) '' convert dec to hex
  15.         intCellRow = intCellRow + 1
  16.     Loop
  17.     Close intFileNum
  18.    
  19. Application.ScreenUpdating = True

  20. MsgBox "Done."

  21. End Sub
複製代碼

作者: quickfixer    時間: 2020-8-28 04:52

回復 3# swatt


    Sub Temp1()
    Dim intFileNum%, byttemp As Byte, intCellRow%, pos
    intFileNum = FreeFile
    intCellRow = 0
   
    fipath = "C:\hello.txt"
   
    Open fipath For Binary Access Read As intFileNum
   
    Do While pos < LOF(intFileNum)
        intCellRow = intCellRow + 1
        Get intFileNum, , byttemp
        pos = Loc(intFileNum)
        Cells(1, intCellRow) = Hex(byttemp)
    Loop
   
    Close intFileNum

MsgBox "Done."

End Sub
作者: swatt    時間: 2020-8-28 10:09

回復 4# quickfixer

這也是個好方法,感謝quickfixer大~




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)