WildByDesign Posted Monday at 11:00 AM Posted Monday at 11:00 AM I am writing an ACL/SDDL parser right now and hit a roadblock. I have to convert a bunch of 32-bit integers into a 32-bit binary value in the form of "000000000000000000000000000000". The vast majority of these 32-bit integers are signed integers (int32) and are converting perfectly with an old function found in the forum: expandcollapse popup; ================================================================================================= ; Func _NumberToBinary($iNumber) ; ; Converts a 32-bit signed # to a binary bit string. (Limitation due to AutoIT functionality) ; NOTE: range for 32-bit signed values is -2147483648 to 2147483647! ; Anything outside the range will return an empty string! ; ; $iNumber = # to convert, obviously ; ; Returns: ; Success: Binary bit string ; Failure: "" and @error set ; ; Author: Ascend4nt, with help from picaxe (Changing 'If BitAND/Else' to just one line) ; See it @ http://www.autoitscript.com/forum/index.php?showtopic=90056 ; ================================================================================================= Func _NumberToBinary($iNumber) Local $sBinString = "" ; Maximum 32-bit # range is -2147483648 to 2147483647 If $iNumber<-2147483648 Or $iNumber>2147483647 Then Return SetError(1,0,"") ; Convert to a 32-bit unsigned integer. We can't work on signed #'s $iUnsignedNumber=BitAND($iNumber,0x7FFFFFFF) ; Cycle through each bit, shifting to the right until 0 Do $sBinString = BitAND($iUnsignedNumber, 1) & $sBinString $iUnsignedNumber = BitShift($iUnsignedNumber, 1) Until Not $iUnsignedNumber ; Was it a negative #? Put the sign bit on top, and pad the bits that aren't set ;If $iNumber<0 Then Return '1' & StringRight("000000000000000000000000000000" & $sBinString,31) ;Return $sBinString $sBinString=StringRight("000000000000000000000000000000" & $sBinString,31) If $iNumber<0 Then Return '1' & $sBinString Return '0' & $sBinString EndFunc ;==>_NumberToBinary The problem now is that I do have a very small number of unsigned integers (uint32) that fail because they are not covered by that function. I spent a few hours searching the forum yesterday and found many examples for converting signed integers, but either no luck finding functions for unsigned integers or the functions were so old that they did not work with newer AutoIt versions. The example unsigned integer that I am stuck on converting right now is "2684354560". Can someone please help me with a function that would convert it to 32-bit binary value format "000000000000000000000000000000"? Thank you for your time.
Solution Nine Posted Monday at 01:17 PM Solution Posted Monday at 01:17 PM (edited) There is numerous converting functions inside ntdll.dll and msvcrt.dll (you can search for it). Here what you want to achieve very easily : ConsoleWrite(_Ui32ToString(0x87654321, 2) & @CRLF) ConsoleWrite(_Ui32ToString(2684354560, 2) & @CRLF) Func _Ui32ToString($i, $base) Return DllCall("ntdll.dll", "str:cdecl", "_ultoa", "ulong", $i, "str", "", "int", $base)[0] EndFunc Edited Monday at 02:22 PM by Nine WildByDesign and KaFu 1 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Multi-Threading Made Easy
WildByDesign Posted Monday at 02:34 PM Author Posted Monday at 02:34 PM 1 hour ago, Nine said: There is numerous converting functions inside ntdll.dll and msvcrt.dll (you can search for it). Here what you want to achieve very easily : Thank you so much for the fast and accurate solution. It works perfectly with everything that I have thrown at it. I was able to combine it with the other function so that it handles signed or unsigned in the same function. Cheers!
Nine Posted Monday at 03:16 PM Posted Monday at 03:16 PM FYI, the sign function inside ntdll.dll : ConsoleWrite(_I32ToString(-5, 2) & @CRLF) Func _I32ToString($i, $base) Return DllCall("ntdll.dll", "str:cdecl", "_itoa", "int", $i, "str", "", "int", $base)[0] EndFunc WildByDesign 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Multi-Threading Made Easy
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