I had recently written a script to convert a String to it’s equivalent in ASCII using the Sample code given in Books Online. Thought I would put it up so that other people could use it as well.
SET TEXTSIZE 0 SET NOCOUNT ON --Declare the Local Variables DECLARE @position int, @string varchar(256),@count int,@rownum int,@output varchar(8000) -- Initialize the Local Variables used to keep track of the current position and offsets SET @position = 1 SET @count = 1 -- Get the data that you want to convert to ASCII select [name],ROW_NUMBER() OVER(ORDER BY [name]) as ROWNUM into #tmp_tbl from sys.syslogins select @rownum = max(rownum) from #tmp_tbl -- Begin WHILE loop to convert the String to ASCII while (@rownum >= @count) begin select @string = [name] from #tmp_tbl where rownum = @count print 'Converting:'+@string WHILE @position <= DATALENGTH(@string) BEGIN SET @output = @output+space(1)+CAST(ASCII(SUBSTRING(@string, @position, 1)) as CHAR(4)) + '|' + CHAR(ASCII(SUBSTRING(@string, @position, 1))) SET @position = @position + 1 END set @position = 1 set @count = @count + 1 print @output set @output = '' end -- Drop the Temporary Table created above drop table #tmp_tbl SET NOCOUNT OFF
A sample output of the above script would be:
116 |t 101 |e 115 |s 116 |t 101 |e 114 |r