(SQL Server) Base62 Encoding and Decoding
Demonstrates base62 encoding and decoding. Note: This example requires Chilkat v11.2.0 or greater.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Base62 encode.
DECLARE @success int
EXEC sp_OAMethod @bd, 'AppendString', @success OUT, 'hello world', 'utf-8'
DECLARE @base62_encoded nvarchar(4000)
EXEC sp_OAMethod @bd, 'GetEncoded', @base62_encoded OUT, 'base62'
PRINT 'hello world --> ' + @base62_encoded
-- Output:
-- hello world --> AAwf93rvy4aWQVw
-- Base62 decode
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @sb, 'DecodeAndAppend', @success OUT, 'AAwf93rvy4aWQVw', 'base62', 'utf-8'
EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
PRINT 'decoded: ' + @sTmp0
-- Output:
-- decoded: hello world
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @sb
END
GO
|