We help IT Professionals succeed at work.

need a script to convert SID from Hex to binary?

Medium Priority
Last Modified: 2012-05-06
can someone please provide a script that will convert a list of SID's from HEX format:

to decimal format:

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


Watch Question

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



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
Most Valuable Expert 2019
Most Valuable Expert 2018
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.
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
End Function
Function EndianReverse(strHex)
  Dim intCounter
  For intCounter = Len(strHex) to 1 Step - 2
    EndianReverse = EndianReverse & Mid(strHex, intCounter - 1, 2)
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


excellent! just what I was looking for.
thanks so much oBda!
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.


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.