The following procedure gets the Ascii Conversion Table and displays all the ASCII code starting from 0 to 255. The procedure takes an int type argument and will show the ASCII code starting from that particular number.
Let's assume if we want to display the ASCII table starting from 100 till 255, then we will call the procedure using
exec convASCII 100
CREATE PROCEDURE convASCII
@counter int -- Argument describing the starting number for the table
AS
DECLARE @col int, @strline varchar(3000) -- strline is going to store the ASCII code table
SET @col = 0
SET @strline = ' '
-- Let's make a loop starting from the counter variable till 255.
WHILE @counter <= 255
begin
We are making the table to display 10 ASCII codes in a row i.e. 10 columns in one row.
WHILE @col < 10
BEGIN
SET @strline = @strline + ' | ' + convert(varchar(4), @counter)+ ' - ' + CHAR(@counter) -- Displaying the ASCII code and the character code in the table. CHAR converts the int ASCII code to character. CONVERT function converts the second argument type to the data type provided in the first argument.
SET @counter=@counter+1 - increment the counter
SET @col = @col + 1 - increment the column
END
SET @strline = CHAR(9) + @strline + ' | ' + CHAR(13) -- Char(9) is the TAB key and CHAR(13) is for Carriage Return
SET @col = 0
end
PRINT @strLine -- Display the ASCII Codes.
Bookmarks