Skip to content

Instantly share code, notes, and snippets.

@amtwo
Last active July 1, 2021 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amtwo/95274e642f1af576fa06f236f8bd373d to your computer and use it in GitHub Desktop.
Save amtwo/95274e642f1af576fa06f236f8bd373d to your computer and use it in GitHub Desktop.
-- Code retrieved on 2021-07-01
-- from https://docs.microsoft.com/en-us/troubleshoot/sql/security/transfer-logins-passwords-between-instances
-- Originally published in knowledge base article KB918992
-- Copyright Microsoft
USE master
GO
IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
DROP PROCEDURE sp_hexadecimal
GO
CREATE PROCEDURE sp_hexadecimal
@binvalue varbinary(256),
@hexvalue varchar (514) OUTPUT
AS
DECLARE @charvalue varchar (514)
DECLARE @i int
DECLARE @length int
DECLARE @hexstring char(16)
SELECT @charvalue = '0x'
SELECT @i = 1
SELECT @length = DATALENGTH (@binvalue)
SELECT @hexstring = '0123456789ABCDEF'
WHILE (@i <= @length)
BEGIN
DECLARE @tempint int
DECLARE @firstint int
DECLARE @secondint int
SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
SELECT @firstint = FLOOR(@tempint/16)
SELECT @secondint = @tempint - (@firstint*16)
SELECT @charvalue = @charvalue +
SUBSTRING(@hexstring, @firstint+1, 1) +
SUBSTRING(@hexstring, @secondint+1, 1)
SELECT @i = @i + 1
END
SELECT @hexvalue = @charvalue
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment