• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

need a script to convert SID from Hex to binary?

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.


  • 2
1 Solution
Ken FayalCommented:
Here's the exact site you want to visit.  Shows how it's done.

siber1Author 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
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

siber1Author Commented:
excellent! just what I was looking for.
thanks so much oBda!

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now