4 posts in this topic
Sort txt file line by line
http://www.****.com/123.php?id=5|http://www.***.com/services/123.php?id=16|http://www.123.com/questions/q.php?id=5|http://123.com/researcher/view_group.php?id=995|http://www.123.com/en/contest/|http://www.123.com/club/123.php?id=77| How can I sort a file like this in to a line by line .txt file?
Parsing binary data frames from an asynchronous source
I was just working on a project that involved decoding a stream of binary data from a serial port in AutoIt. It took me a few hours to figure out how to process the data efficiently in AutoIt and I did not find any helpful examples on how to do so, so I thought I would share my core example and maybe save someone else some time. There may be a more efficient way to do this, but this works well for me.
#cs Author: ToasterKing This is an example of a way to parse streaming binary data that follows a strict format with a header and footer. In this example, each frame is 5 bytes with a 2-byte header of 0xD5AA and a 1-byte footer of 0xAD. The _BinaryParse() function accumulates incoming data in a buffer. Once a footer is found, it searches backward for the header, and if it is in the right position, it extracts the remaining 2 bytes in the middle, then moves on to looking for the next frame. #ce ; The data source might be something asynchronous like serial or TCP, but since this is just an example, I'm just putting the data in a variable. Local $fSomeData $fSomeData = Binary("0xD5AA24B1") ; Binary data constituting almost a complete frame. _BinaryParse($fSomeData) ; Call the function with the received data. It isn't a complete frame, so it is just stored in the buffer until more data is received. $fSomeData = Binary("0xAD62D5AA92E7AD") ; Remainder of the previous frame, one garbage byte (0x62) which should be skipped, and a complete additional frame. _BinaryParse($fSomeData) ; The function should be able to parse both frames now. Func _BinaryParse($fNewData) Local Static $fBinaryReceived = Binary("") ; Buffer for received data ConsoleWrite("Hey, the function is called!" & @CRLF) ; Add new data to the buffer. ; This ridiculous monstrosity is the only way I could find to append binary data to binary data in AutoIt. It must be converted to strings first. ; Both, one, or no substrings will begin with "0x" depending on whether they contained binary data. To be converted back to binary properly, only one instance ; of "0x" must exist at the beginning of the string. $fBinaryReceived = Binary("0x" & StringReplace(String($fBinaryReceived) & String($fNewData),"0x","")) ConsoleWrite("Data in the buffer: " & String($fBinaryReceived) & @CRLF) Local $iLength = BinaryLen($fBinaryReceived) ; Count the bytes in the data If $iLength > 0 Then Local $fBinaryReceivedTemp = $fBinaryReceived ; Create temporary copy to work on Local $fByte1,$fByte2 For $i = 1 To $iLength If BinaryMid($fBinaryReceivedTemp,$i,1) = 0xAD Then ; If the 1-byte footer found ConsoleWrite("Footer found at end of " & $i & " of " & $iLength & " bytes!" & @CRLF) If BinaryMid($fBinaryReceivedTemp,$i - 4,1) = 0xD5 And BinaryMid($fBinaryReceivedTemp,$i - 3,1) = 0xAA Then ; and the 2-byte header is found 4 bytes before that ConsoleWrite("Header found before the footer!" & @CRLF) $fByte1 = BinaryMid($fBinaryReceivedTemp,$i - 2,1) ; Get 1st byte in the body (between header and footer) $fByte2 = BinaryMid($fBinaryReceivedTemp,$i - 1,1) ; Get 2nd byte in the body (between header and footer) ConsoleWrite("Here is the critical data: " & String($fByte1) & " " & String($fByte2) & @CRLF) ; Just display the 2 bytes for demonstration purposes. Normally, you'd do something more useful with it here. EndIf $fBinaryReceived = BinaryMid($fBinaryReceivedTemp,$i + 1) ; Truncate the original data to remove all of the bytes just processed, then continue processing $fBinaryReceivedTemp EndIf Next EndIf EndFunc
_GDIPlus and Buttons gone after minimize restore
i have a code that creates buttons and graphics in the same block. before the block is _GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($mainGUI) $hBrush = _GDIPlus_BrushCreateSolid(0xF03FC744) ; Felish Green colour $hPen = _GDIPlus_PenCreate(0xFFBFCC49); Granite colour $hPath = _GDIPlus_PathCreate() ;Create new path object $hFamily = _GDIPlus_FontFamilyCreate("Tahoma") ;Create font family object $tLayout = _GDIPlus_RectFCreate($startX, $startY, $bHeigh, $bWidth) ;Create string bounding rectangle X=0, Y=0 $hFormat = _GDIPlus_StringFormatCreate(0x0400); Allow overhanging rectangle _GDIPlus_StringFormatSetAlign($hFormat, 1); Align text horizontally _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ; Align text Vertically ; Add String like CtrlLabels assign to rectangle and formats including Font _ _GDIPlus_PathAddString($hPath, FileReadLine($configF, $SN), $tLayout, $hFamily, 0, 26, $hFormat) _GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;Sets the graphics object rendering quality (antialiasing) _GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush) ; Fill path to graphics handle (GUI) end of the block
_GDIPlus_BrushDispose($hBrush) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_PenDispose($hPen) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() now this works on the first run but when i minimize and then restore _GDIPlus_ is gone and so are buttons. but if i create labels they stay.
i've tried moving "Dispose" functions at the end of the loop but still the same result
Problems with arrays
In my current project I have a part that should convert from string to array, and later on that array should be converted to a string again.
The first part works just fine and I get the array just as I want it, but on the way back I'm encountering some issues...
Func Encode($data0) ; works just fine, just here for clarification. ; That if-statement in the middle is just there that I'm always getting a string with 5 symbols ; $data0 = "A" in this example Local $data1 = "" Local $tmp = "" Local $arr = StringSplit($data0, "", 2) For $i = 0 To UBound($arr)-1 $tmp = AscW($arr[$i]) If $tmp = 0 Then $tmp = "00000" & $tmp ElseIf $tmp > 0 And $tmp < 10 Then $tmp = "0000" & $tmp ElseIf $tmp >= 10 And $tmp < 100 Then $tmp = "000" & $tmp ElseIf $tmp >= 100 And $tmp < 1000 Then $tmp = "00" & $tmp ElseIf $tmp >= 1000 And $tmp < 10000 Then $tmp = "0" & $tmp ElseIf $tmp >= 10000 And $tmp <= 65535 Then $tmp = $tmp EndIf $data1 = $data1 & $tmp Next Return $data1 EndFunc Func Decode($data0) ; does NOT work ; $data0 = "00065" ; via MsgBox and ConsoleWrite I could find out that it fails already before StringSplit ; when it fails, no message is given and everything just disappears Local $data1 = "" Local $tmp = "" Local $arr = StringSplit(String($data0), "", 2) For $i = 0 To UBound($arr)-1 Step 5 $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4] & $arr[$i+5] $tmp = ChrW($tmp) $data1 = $data1 & $tmp Next Return $data1 EndFunc Sorry for dropping all the #include, the GUI, the login screen, etc... It's not necessary for this problem and I needn't give you a 613kB file full of plain text for finding an error in 3 lines of code.
I found out that $arr is always empty, so there must be an issue with StringSplit. I suspected "00065" might be seen as an integer and converted it to a string, but that doesn't make it work. I already played around quite a while with that issue now, I hope some of you can finally relieve me.
Problem with FileCopy()
I'm writing a small program for creating a backup. I want to do that by using FileCopy. Everything from drive C should be copied to drive D.
FileCopy("C:", "D:", $FC_OVERWRITE + $FC_CREATEPATH) That doesn't do anything, though. I played a bit with the paths, but nothing does what I want. Sometimes only the folder of the program itself is copied, sometimes nothing is copied.
What am I doing wrong? What do I have to do that everything from C goes to D? Thanks!
Edit: DirCopy does pretty much the same