Jump to content

Need help converting uint32 to 32-bit binary


Go to solution Solved by Nine,

Recommended Posts

Posted

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:

; =================================================================================================
; 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
Posted (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 by Nine
Posted
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!

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...