ALTER function Varbin2hexstr (@bin varbinary (8000)) returns varchar (8000) asBEGIN DECLARE @re varchar (8000), @iint Select@re ="', @i=datalength (@bin) while@i>0 Select@re =substring ('0123456789ABCDEF', substring (@bin, @i,1)/ -+1,1) +substring ('0123456789ABCDEF', substring (@bin, @i,1)% -+1,1) +@re, @i[Email protected]1--return('0x'+@re)return@re End
ALTER function [dbo]. [Hexstr2varbin] (@char varchar (8000)) returns varbinary (8000) asBEGIN DECLARE @re varbinary (8000), @tempchar varchar (2), @getchar varchar (1), @getintint, @nint, @totalintint, @iint, @tempintint, @runNumint--number of string intercept segmentsSelect@tempchar ="', @i=datalength (@char), @re =0x; if(@i>0) Beginif(@i%2=0)Set@runNum = @i/2 Else Set@runNum = @i/2+1 while(@runNum >0) Beginif(@runNum =1)Set@tempchar =@charElse Set@tempchar = substring (@char, (@runNum-1)*2,2) Select@n=1, @totalint =0; --Loop through each string that is intercepted (the string length here is 2) while@n < (datalength (@tempchar) +1) BeginSet@getchar =substring (@tempchar, @n,1); --converts a character to a hexadecimal corresponding numberSelect@getint = CaseWhen @getchar='a'ThenTenWhen @getchar='b'Then OneWhen @getchar='C'Then AWhen @getchar='D'Then -When @getchar='e'Then -When @getchar='F'Then the ElseConvertint, @getchar) end; Set@[email Protected]*power ( -, Datalength (@tempchar)-@n)Set@totalint = @totalint +@tempintSet@[email protected]+1EndSet@re =convert (varbinary (1), @totalint) +@re; Set@[email protected]1; End Endreturn@reend
SQL varbinary varchar Mutual transfer