Artisan Posted July 20, 2008 Share Posted July 20, 2008 (edited) AutoIt version: v3.2.12.1 OS: Win2K Scripting in: SciTE4AutoIt3 Version 1.76 I'm writing a script that needs to take a number (any format), and return it in reverse byte order. Simple enough, I thought. In the course of bug testing, I removed a rather critical part of my function... and it still worked! (I'm looking specifically at padding with extra 0's where needed.) Don't get me wrong - that's really cool that it still works, but I don't want to rely on a bug or other weirdness. Here's my code: expandcollapse popup; Switch a number of any type to hex reverse byte order ; Will also pad with 0's as needed ; Note that truncation will drop the highest order bytes $bin = ReverseBytes(0xEFCDAB, 6) ConsoleWrite($bin & @LF); = 0xABCDEF (normal) $bin = ReverseBytes("0xABCDEF", 4) ConsoleWrite($bin & @LF); = 0xEFCD (truncated) $bin = ReverseBytes(200, 6) ConsoleWrite($bin & @LF); = 0xC80000 (padded) $bin = ReverseBytes("57082", 4) ConsoleWrite($bin & @LF); = 0xFADE (normal) ;#cs -- The Original Version Func ReverseBytes ( Const $Val, Const $NumBytes ) Local $Data = StringMid(Binary(Int($Val)), 1, $NumBytes + 2) While StringLen($Data) < $NumBytes + 2 $Data &= "0"; Padding as needed WEnd Return $Data EndFunc ;#ce #cs -- Why does this add byte padding too?? Func ReverseBytes ( Const $Val, Const $NumBytes ) Return StringMid(Binary(Int($Val)), 1, $NumBytes + 2) EndFunc #ce #cs -- Just in case, but this way adds byte padding as well... Func ReverseBytes ( Const $Val, Const $NumBytes ) $Data1 = Int($Val) $Data2 = Binary($Data1) $Data3 = StringMid($Data2, 1, $NumBytes + 2) Return $Data3 EndFunc #ce No matter which version of the ReverseBytes function I use, I always get byte padding. Is this the same for everyone else? Can someone help me figure out why it gives byte padding when non has been specifically coded? No help file or forum searches have turned up an answer to this yet. (In case it's no immediately clear, the Binary() function itself reverses the order of the bytes.) EDIT - Solved. Edited July 20, 2008 by Artisan Link to comment Share on other sites More sharing options...
DW1 Posted July 20, 2008 Share Posted July 20, 2008 $bin = ReverseBytes(200, 6) = gives byte padding because you told it you want 6 bytes. $bin = ReverseBytes(200, 2) = No byte padding AutoIt3 Online Help Link to comment Share on other sites More sharing options...
Artisan Posted July 20, 2008 Author Share Posted July 20, 2008 That's true, but try with the 2nd version of ReverseBytes. It doesn't have the coding to add any padding at all, yet it acts like it does. When using StringMid() - the function that seems to be adding the padding - if the 3rd parameter (count) puts it beyond the end of the string, it will just stop at the end of the string. Why is it adding 0's anyway? Link to comment Share on other sites More sharing options...
DW1 Posted July 20, 2008 Share Posted July 20, 2008 (edited) You could always just change the function to return the way you want. Maybe something like this? $bin = ReverseBytes(0xEFCDAB) ConsoleWrite($bin & @LF); = 0xABCDEF (normal) $bin = ReverseBytes("0xABCDEF",1) ConsoleWrite($bin & @LF); = 0xEFCD (truncated) $bin = ReverseBytes(200) ConsoleWrite($bin & @LF); = 0xC80000 (padded) $bin = ReverseBytes("57082") ConsoleWrite($bin & @LF); = 0xFADE (normal) Func ReverseBytes ( Const $Val, Const $Truncate = 0 ) Local $Data = Binary(Int($Val)) While StringRight($data, 2) = "00" $data = StringTrimRight( $data, 2 ) WEnd $data = StringTrimRight( $data, $truncate * 2 ) Return $Data EndFunc ReversedBytes( <Bytes To Reverse>, <number of bytes to truncate if any>) Edited July 20, 2008 by danwilli AutoIt3 Online Help Link to comment Share on other sites More sharing options...
Artisan Posted July 20, 2008 Author Share Posted July 20, 2008 Thanks for the fast responses, danwilli. Actually, I think I just figured it out. It seems when you call Binary() within StringMid(), the Binary() function allows up to 8 characters to be returned, which is why they're accessible to StringMid(). Try this: ConsoleWrite(StringMid(Binary(0xABCD), 3, 10) & @LF) ; = CDAB0000 I'm asking to get 10 bytes back, but it caps out at 8. The first version of my function works as expected, however, padding in the extra 0's. Why do I always wait to solve my own problems until after I've already posted? muttley Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now