rabbitkillrun2 Posted June 24, 2011 Share Posted June 24, 2011 Hey, I am trying to create packets to send over TCP. The first 4 bytes describe the length, in bytes, of the packet data after these 4 bytes. I use StringLen() to get the length (which may be a bad idea since I will probably end up wanting to send more than just text), which gives me an Int. I then need to convert this to binary/hex (I still get confused when dealing with these in AutoIt), but it has to be in little endian format, and Hex() returns big endian. I found this function while searching, but I haven't tried it yet as I'd rather check that I was doing it properly, and maybe get some help from someone who knows more about this than me. Func SwapEndian($hex) Return Hex(BitOR(BitOR(BitOR(BitShift($hex, 24), _ BitAND(BitShift($hex, -8), 0x00FF0000)), _ BitAND(BitShift($hex, 8), 0x0000FF00)), _ BitShift($hex, -24)), 8) EndFunc;==>SwapEndian Thanks rak Link to comment Share on other sites More sharing options...
Shaggi Posted June 24, 2011 Share Posted June 24, 2011 Hey, I am trying to create packets to send over TCP. The first 4 bytes describe the length, in bytes, of the packet data after these 4 bytes. I use StringLen() to get the length (which may be a bad idea since I will probably end up wanting to send more than just text), which gives me an Int. I then need to convert this to binary/hex (I still get confused when dealing with these in AutoIt), but it has to be in little endian format, and Hex() returns big endian. I found this function while searching, but I haven't tried it yet as I'd rather check that I was doing it properly, and maybe get some help from someone who knows more about this than me. Func SwapEndian($hex) Return Hex(BitOR(BitOR(BitOR(BitShift($hex, 24), _ BitAND(BitShift($hex, -8), 0x00FF0000)), _ BitAND(BitShift($hex, 8), 0x0000FF00)), _ BitShift($hex, -24)), 8) EndFunc;==>SwapEndian Thanks rak Do you use TCPSend or something? Usually, you can just do like this: $iLen = StringLen($Data) $aPacket = "0x" & _ "9F" & _ "4B" & _ ;Consider this random packet data SwapEndian($iLen) & _ $Data & _ "00" SendPacket($aPacket) Func SwapEndian($Data) Return Hex(Binary($Data)) EndFunc Or, if you need to pass a pointer to a packet, do like this: $iLen = StringLen($Data) $aPacket = "0x" & _ ; Necessary for dll struct trick to work "9F" & _ "4B" & _ ;Consider this random packet data SwapEndian($iLen) & _ $Data & _ "00" $PacketStruct = DllStructCreate("byte[" & $PacketSize & "]") ;$PacketStruct = DllStructCreate("byte[" & BinaryLen($aPacket) & "]") DllStructSetData($PacketStruct,1,$aPacket) $PacketPtr = DlLStructGetPtr($PacketStruct) SendPacket($PacketPtr) Func SwapEndian($Data) Return Hex(Binary($Data)) EndFunc Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG Link to comment Share on other sites More sharing options...
rabbitkillrun2 Posted June 24, 2011 Author Share Posted June 24, 2011 Thanks for the post. Where did you get 9F, 48 and 00 from though? Yes, I am using TCPSend. Link to comment Share on other sites More sharing options...
Shaggi Posted June 24, 2011 Share Posted June 24, 2011 Thanks for the post.Where did you get 9F, 48 and 00 from though?Yes, I am using TCPSend.;Consider this random packet dataIt was just an example of how the packet could look like, it's irrelevant. Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG Link to comment Share on other sites More sharing options...
rabbitkillrun2 Posted June 24, 2011 Author Share Posted June 24, 2011 Ah, I seem to miss end of line comments a lot, probably because they're so light. Sorry. So Hex(Binary($Data)) will swap the endian? That's a useful trick. Could you help me with Hex() and Binary()? The difference between them is Hex() will only do up to 8 characters and returns a string representation of binary, but Binary() will do any length and returns binary data? Would Binary(8) return the number 8 in binary form, and Binary("8 as a string") return that string in binary form in ansi? Link to comment Share on other sites More sharing options...
jchd Posted June 25, 2011 Share Posted June 25, 2011 Seems to me you're mixing up a few things.First, as he helpfile says, Hex(expr) will return the string representation of an integer or of a binary type converted to hexadecimal.As a consequence, NO, Hex(Binary(expr)) does not swap endiannness. Strings don't have endianness per se. Only UTF-16 and UTF-32 encodings are subject to endianness (their code units can be LE or BE). This is unrelated to the NUXI syndrome, technically speaking.Binary(expr) will return a binary type.String and binary types are not the same beast.PC OSes (and processors) are little-endian.Now, regarding LE vs. BE, endianness applies to determined types, or to put things otherwise, applies to explicit integer type sizes.Your best bet to avoid getting wrong is to setup a DllStruct with required types, then fill it using the endianness required by the protocol you're using and feed the content to network I/O. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
rabbitkillrun2 Posted June 26, 2011 Author Share Posted June 26, 2011 This bit confused me: Func SwapEndian($Data) Return Hex(Binary($Data)) EndFunc I've never worked with DLLStructs before. :/ Could you provide an example on how to do this? As for the endianess bit, I was trying to make something that would take an integer,for example, 10, and return something like this 0x0A000000. Link to comment Share on other sites More sharing options...
jchd Posted June 26, 2011 Share Posted June 26, 2011 There are too many examples of Dll* functions usage both in the help file, in the examples inside it and on the forumS. So search a bit by yourself and chime again if you're get stuck. PC is definitely LE This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Shaggi Posted July 4, 2011 Share Posted July 4, 2011 (edited) Seems to me you're mixing up a few things. First, as he helpfile says, Hex(expr) will return the string representation of an integer or of a binary type converted to hexadecimal.As a consequence, NO, Hex(Binary(expr)) does not swap endiannness. Strings don't have endianness per se. Only UTF-16 and UTF-32 encodings are subject to endianness (their code units can be LE or BE). This is unrelated to the NUXI syndrome, technically speaking.Binary(expr) will return a binary type.String and binary types are not the same beast.PC OSes (and processors) are little-endian.Now, regarding LE vs. BE, endianness applies to determined types, or to put things otherwise, applies to explicit integer type sizes. Your best bet to avoid getting wrong is to setup a DllStruct with required types, then fill it using the endianness required by the protocol you're using and feed the content to network I/O. $Hex = 0xAABBFFEE ConsoleWrite("0x" & Hex($Hex) & @CRLF) ConsoleWrite("0x" & Hex(Binary($Hex)) & @CRLF)The endianess is swapped. This is very useful if you work with binary code. This is ofcourse strings, so to use this, you need to autoset it in byte[] dllstructs. Ah, I seem to miss end of line comments a lot, probably because they're so light. Sorry. So Hex(Binary($Data)) will swap the endian? That's a useful trick. Could you help me with Hex() and Binary()? The difference between them is Hex() will only do up to 8 characters and returns a string representation of binary, but Binary() will do any length and returns binary data? No problem Yes it will. Hex just formats an integer to a string representation. Would Binary(8) return the number 8 in binary form, and Binary("8 as a string") return that string in binary form in ansi?Double yes. Edited July 4, 2011 by Shaggi Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG 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