Script for printing out ASCII characters


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.
 
<<SCRIPT>>
 

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 

 

<</SCRIPT>
 
A sample output of the above script would be:
Converting: tester 

116 |t 101 |e 115 |s 116 |t 101 |e 114 |r 

 

Advertisements

It is always good to hear from you! :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s