We help IT Professionals succeed at work.

need a script to convert SID from Hex to binary?

Medium Priority
1,068 Views
Last Modified: 2012-05-06
can someone please provide a script that will convert a list of SID's from HEX format:
X'010500000000000515000000492e24037d1903770052cf126ea20000'

to decimal format:
S-1-5-21-207860035-1181422338--730112994-1007

I have over 400 to convert, so a script that would call an input text or csv file would be ideal.

thanks!

Mike
Comment
Watch Question

Commented:
Here's the exact site you want to visit.  Shows how it's done.

http://froosh.wordpress.com/2005/10/21/hex-sid-to-decimal-sid-translation/

Author

Commented:
thanks. but i was already aware of that link before I posted this. That code that is posted doesn't seem to work for me.
does anyone else have another script i can try.
again, i have to convert 300+ SID's from hex to decimal.

thx - Mike
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Save the condensed version below as Hex2Sid.vbs. The script expects the hex string to to convert as argument, and will echo out the hex string, an equal sign, and the sid string.
010500000000000515000000492e24037d1903770052cf126ea20000=S-1-5-21-52702793-1996691837-315576832-41582
Run this using cscript for several conversions using the command line and a list of input hex string (hex.txt) with the following command:
for /f %a in ('type hex.txt') do @cscript /nologo Hex2Sid.vbs %a
You can capture the output by redirecting it to a file (sid.txt):
for /f %a in ('type hex.txt') do @cscript /nologo Hex2Sid.vbs %a>>"sid.txt"

Function ConvertHexStringToSidString(strHex)
  Const intSidVersionLength = 2
  Const intSubAuthorityCountLength = 2
  Const intAuthorityIdentifierLength = 12
  Const intSubAuthorityLength = 8
  Dim intStringPosition, bytSidVersion, lngAuthorityIdentifier, bytSubAuthorityCount, lngTempSubAuthority
  intStringPosition = 1
  bytSidVersion = CByte("&h" & Mid(strHex, intStringPosition, intSidVersionLength))
  intStringPosition = intStringPosition + intSidVersionLength
  bytSubAuthorityCount = CByte("&h" & Mid(strHex, intStringPosition, intSubAuthorityCountLength))
  intStringPosition = intStringPosition + intSubAuthorityCountLength
  lngAuthorityIdentifier = CLng("&h" & Mid(strHex, intStringPosition, intAuthorityIdentifierLength))
  intStringPosition = intStringPosition + intAuthorityIdentifierLength
  ConvertHexStringToSidString = "S-" & bytSidVersion & "-" & lngAuthorityIdentifier
  Do Until bytSubAuthorityCount = 0
    lngTempSubAuthority = CLng("&h" & EndianReverse(Mid(strHex, intStringPosition, intSubAuthorityLength)))
    intStringPosition = intStringPosition + intSubAuthorityLength
    If lngTempSubAuthority < 0 Then lngTempSubAuthority = lngTempSubAuthority + 2^32
    ConvertHexStringToSidString = ConvertHexStringToSidString & "-" & lngTempSubAuthority
    bytSubAuthorityCount = bytSubAuthorityCount - 1
  Loop
End Function
 
Function EndianReverse(strHex)
  Dim intCounter
  For intCounter = Len(strHex) to 1 Step - 2
    EndianReverse = EndianReverse & Mid(strHex, intCounter - 1, 2)
  Next
End Function
 
' ----- Main
strHex = wscript.Arguments(0)
strSid = ConvertHexStringToSidString(strHex)
wscript.echo strHex & "=" & strSid

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
excellent! just what I was looking for.
thanks so much oBda!
Mike
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.