Exctrating data from binary file - NEWBIE :(

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Exctrating data from binary file - NEWBIE :(

Post by Demivec »

Try using it this way:

Code: Select all

...
ElseIf EventGadget = #Button_4
Result$ = calculation()
SetGadgetText(#Text_6,Result$)
The procedure returns a string value but you are not receiving that return value anywhere (i.e. in a string variable).
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Exctrating data from binary file - NEWBIE :(

Post by breeze4me »

Code: Select all

EnableExplicit

Structure AscArray
  a.a[0]
EndStructure

Global *buffer.AscArray   ; *buffer\a[offset]

Procedure OpenFileAndReadData()
  Protected FileBytes.q, FileNum, File$, result
  File$ = OpenFileRequester("Please choose file to load", "DATA.bin", "bin (*.bin)|*.bin", 0)
  If File$
    FileNum = ReadFile(#PB_Any, File$)
    If FileNum
      FileBytes = Lof(FileNum)
      
      If *buffer : FreeMemory(*buffer) : EndIf
      
      *buffer = AllocateMemory(FileBytes)
      If *buffer
        ReadData(FileNum, *buffer, FileBytes)
        result = 1
      EndIf
      CloseFile(FileNum)
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

;--------------------------------------
Procedure.s calculation1()
  Protected.a data1, data2, data3, data4
  Protected Result$
  
  data1 = (*buffer\a[$1E] ! $10) + $25
  data2 = (*buffer\a[$1F] ! $10) + $25
  data3 = (*buffer\a[$20] ! $10) + $25
  data4 = (*buffer\a[$21] ! $10) + $25
  
  ;Result$ = Hex(data1) + Hex(data2) + Hex(data3) + Hex(data4)
  Result$ = RSet(Hex(data1), 2, "0") + RSet(Hex(data2), 2, "0") + RSet(Hex(data3), 2, "0") + RSet(Hex(data4), 2, "0")
  ProcedureReturn Result$
EndProcedure

;--------------------------------------
Procedure getByte(*buffer, offset) ;this procedure is just for convenience and readability
  ProcedureReturn PeekB(*buffer + offset)
EndProcedure

Procedure.s calculation2()
  Protected.a data1, data2, data3, data4
  Protected Result$
  data1 = (getByte(*buffer, $1E) ! $10) + $25
  data2 = (getByte(*buffer, $1F) ! $10) + $25
  data3 = (getByte(*buffer, $20) ! $10) + $25
  data4 = (getByte(*buffer, $21) ! $10) + $25
  
  ;Result$ = Hex(data1, #PB_Byte )+Hex(data2, #PB_Byte )+Hex(data3, #PB_Byte )+Hex(data4, #PB_Byte )
  Result$ = RSet(Hex(data1), 2, "0") + RSet(Hex(data2), 2, "0") + RSet(Hex(data3), 2, "0") + RSet(Hex(data4), 2, "0")
  ProcedureReturn Result$
EndProcedure

;--------------------------------------
Procedure.s calculation3()
  Protected.a data1, data2, data3, data4
  Protected Result$
  
  data1 = (PeekB(*buffer + $1E) ! $10) + $25
  data2 = (PeekB(*buffer + $1F) ! $10) + $25
  data3 = (PeekB(*buffer + $20) ! $10) + $25
  data4 = (PeekB(*buffer + $21) ! $10) + $25
  
  ;Result$ = Hex(data1, #PB_Byte)+Hex(data2, #PB_Byte)+Hex(data3, #PB_Byte)+Hex(data4, #PB_Byte)
  Result$ = RSet(Hex(data1), 2, "0") + RSet(Hex(data2), 2, "0") + RSet(Hex(data3), 2, "0") + RSet(Hex(data4), 2, "0")
  ProcedureReturn Result$
EndProcedure




Enumeration 
  #txtValue1
  #txtValue2
  #txtValue3
  #btnOpen
  #btnShow
EndEnumeration

Define IsReady, event, eg, et

If OpenWindow(0, 0, 0, 200, 150, "test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  TextGadget(#txtValue1, 10, 10, 100, 25, "", #PB_Text_Border)
  TextGadget(#txtValue2, 10, 50, 100, 25, "", #PB_Text_Border)
  TextGadget(#txtValue3, 10, 90, 100, 25, "", #PB_Text_Border)
  ButtonGadget(#btnOpen, 130, 10, 50, 25, "Open")
  ButtonGadget(#btnShow, 130, 50, 50, 25, "Show")
  
  Repeat
    event = WaitWindowEvent()
    
    If event = #PB_Event_Gadget
      eg = EventGadget()
      et = EventType()
      
      If eg = #btnOpen And et = #PB_EventType_LeftClick
        If OpenFileAndReadData()
          IsReady = 1
        Else
          IsReady = 0
        EndIf
        SetGadgetText(#txtValue1, "")
        SetGadgetText(#txtValue2, "")
        SetGadgetText(#txtValue3, "")
        
      ElseIf eg = #btnShow And et = #PB_EventType_LeftClick
        If IsReady  ;or  If *buffer    ; must test it !  
          SetGadgetText(#txtValue1, calculation1()) ; 1480D775 (with your DATA.bin file)
          SetGadgetText(#txtValue2, calculation2()) ; 1480D775 (with your DATA.bin file)
          SetGadgetText(#txtValue3, calculation3()) ; 1480D775 (with your DATA.bin file)
        Else
          SetGadgetText(#txtValue1, "No data")
        EndIf
        
      EndIf
    EndIf
  Until event = #PB_Event_CloseWindow
  
EndIf
This code shows three methods.
I got no errors.
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Exctrating data from binary file - NEWBIE :(

Post by Vitor_Boss® »

Try using this:

Code: Select all

...
ElseIf EventGadget = #Button_4
SetGadgetText(#Text_6, calculation())
I wrote this procedure to read binary files in an Array, try use this faster procedure:

Code: Select all

EnableExplicit
Global Dim Bytes.a(1)

Procedure GetByte(FileName.s,Array Output.a(1), Size=0, START=0); by Vitor_Boss®
  Protected FileHandle, Result
  If FileSize(FileName) > 0
    FileHandle = OpenFile(#PB_Any,FileName)
    If FileHandle <> 0
      If Size = 0
        Size = Lof(FileHandle)
      EndIf
      ReDim Output(Size)
      If ArraySize(Output.a()) <> -1
        FileSeek(FileHandle, START)
        ReadData(FileHandle, @Output(0), Size)
        Result=#True
      EndIf
      CloseFile(FileHandle)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure
Procedure OpenFileAndReadData()
  Protected File$, result
  File$ = OpenFileRequester("Please choose file to load", "DATA.bin", "bin (*.bin)|*.bin", 0)
  If File$
    ProcedureReturn GetByte(File$, Bytes()); by Vitor_Boss®
  EndIf
EndProcedure
Procedure.s calculation(); by Vitor_Boss®
  Protected.s data1, data2, data3, data4    
  data1 = RSet(Hex((Bytes($1E) ! $10) + $25, #PB_Byte), 2, "0"); by breeze4me
  data2 = RSet(Hex((Bytes($1F) ! $10) + $25, #PB_Byte), 2, "0")
  data3 = RSet(Hex((Bytes($20) ! $10) + $25, #PB_Byte), 2, "0")
  data4 = RSet(Hex((Bytes($21) ! $10) + $25, #PB_Byte), 2, "0")
  ProcedureReturn data1 + " " + data2 + " " + data3 + " " + data4
EndProcedure

;---------------- by breeze4me ------------------------
Enumeration 
  #txtValue1
  #btnOpen
  #btnShow
EndEnumeration

Define IsReady, event, eg, et

If OpenWindow(0, 0, 0, 185, 70, "EEPROM Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  TextGadget(#txtValue1, 10, 20, 100, 25, "", #PB_Text_Border)
  ButtonGadget(#btnOpen, 130, 5, 50, 25, "Open")
  ButtonGadget(#btnShow, 130, 35, 50, 25, "Show")
  
  Repeat
    event = WaitWindowEvent()
    
    If event = #PB_Event_Gadget
      eg = EventGadget()
      et = EventType()
      
      If eg = #btnOpen And et = #PB_EventType_LeftClick
        IsReady = OpenFileAndReadData()
        SetGadgetText(#txtValue1, "")
        
      ElseIf eg = #btnShow And et = #PB_EventType_LeftClick
        If IsReady  ;or  If *buffer    ; must test it !  
          SetGadgetText(#txtValue1, calculation()) ; 11 80 D7 75 (with your DATA.bin file)
        Else
          SetGadgetText(#txtValue1, "No data")
        EndIf        
      EndIf
    EndIf
  Until event = #PB_Event_CloseWindow  
EndIf
Last edited by Vitor_Boss® on Thu Jan 27, 2011 1:43 am, edited 1 time in total.
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Exctrating data from binary file - NEWBIE :(

Post by breeze4me »

Vitor_Boss® wrote:

Code: Select all

 ; 11 80 D7 75 (with your DATA.bin file)
"11" 80 D7 75 ?
So I have investigated that strange result.
RSet() (PB help file)
...
Example:
Debug RSet("LongString", 4) ; will display "Long"

Code: Select all

Procedure.s calculation()
  Protected.s data1, data2, data3, data4
  
  ;Bytes($1E) = $FF
  Debug Hex((Bytes($1E) ! $10) + $25, #PB_Byte)   ;14
  Debug Hex((Bytes($1E) ! $10) + $25, #PB_Ascii)  ;14
  Debug ""
  Debug Hex((Bytes($1E) ! $10) + $25, #PB_Long)   ;114
  Debug Hex((Bytes($1E) ! $10) + $25)   ;114
  Debug ""
  Debug RSet(Hex((Bytes($1E) ! $10) + $25), 2, "0") ;11
  Debug RSet(Hex((Bytes($1E) ! $10) + $25), 4, "0") ;0114
  
  data1 = RSet(Hex((Bytes($1E) ! $10) + $25), 2, "0")
  data2 = RSet(Hex((Bytes($1F) ! $10) + $25), 2, "0")
  data3 = RSet(Hex((Bytes($20) ! $10) + $25), 2, "0")
  data4 = RSet(Hex((Bytes($21) ! $10) + $25), 2, "0")  
  ProcedureReturn data1 + " " + data2 + " " + data3 + " " + data4
EndProcedure
You must change

Code: Select all

data1 = RSet(Hex((Bytes($1E) ! $10) + $25), 2, "0")
data2 = RSet(Hex((Bytes($1F) ! $10) + $25), 2, "0")
data3 = RSet(Hex((Bytes($20) ! $10) + $25), 2, "0")
data4 = RSet(Hex((Bytes($21) ! $10) + $25), 2, "0")
into

Code: Select all

data1 = RSet(Hex((Bytes($1E) ! $10) + $25, #PB_Byte), 2, "0")
data2 = RSet(Hex((Bytes($1F) ! $10) + $25, #PB_Byte), 2, "0")
data3 = RSet(Hex((Bytes($20) ! $10) + $25, #PB_Byte), 2, "0")
data4 = RSet(Hex((Bytes($21) ! $10) + $25, #PB_Byte), 2, "0")
;or  .... + $25, #PB_Ascii), ....
Besides, the second code is much faster.
Vitor_Boss®
User
User
Posts: 81
Joined: Thu Sep 23, 2010 4:22 am

Re: Exctrating data from binary file - NEWBIE :(

Post by Vitor_Boss® »

breeze4me wrote:
Vitor_Boss® wrote:

Code: Select all

 ; 11 80 D7 75 (with your DATA.bin file)
"11" 80 D7 75 ?
So I have investigated that strange result.
...
Besides, the second code is much faster.
What code?
You mean my code?
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Exctrating data from binary file - NEWBIE :(

Post by breeze4me »

Vitor_Boss® wrote: What code?
You mean my code?
Besides, the second code is much faster.
The replaced code is faster than your original code.
i.e.

Code: Select all

...
data1 = RSet(Hex((Bytes($1E) ! $10) + $25, #PB_Byte), 2, "0")
data2 = RSet(Hex((Bytes($1F) ! $10) + $25, #PB_Byte), 2, "0")
data3 = RSet(Hex((Bytes($20) ! $10) + $25, #PB_Byte), 2, "0")
data4 = RSet(Hex((Bytes($21) ! $10) + $25, #PB_Byte), 2, "0")
;or  .... + $25, #PB_Ascii), ....
...
Post Reply