Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Unicode Escape and Unescape Text in StringBuilder

Demonstrates options for unicode escaping non-us-ascii chars and emojis.

Note: This example requires Chilkat v11.1.0 or greater.

For more information, see https://www.chilkatsoft.com/unicode_escape.asp

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- 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 @success int
    SELECT @success = 0

    DECLARE @sbOriginal int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbOriginal OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @sbOriginal, 'LoadFile', @success OUT, 'qa_data/txt/utf16_emojis_accented_jap.txt', 'utf-16'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sbOriginal, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sbOriginal
        RETURN
      END

    -- The above file contains the following text, which includes some emoji's,
    -- Japanese chars, and accented chars.

    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @sb, 'AppendSb', @success OUT, @sbOriginal

    -- Charset is not used for unicode escaping.  Set it to "utf-8", but it means nothing.
    DECLARE @charsetNotUsed nvarchar(4000)
    SELECT @charsetNotUsed = 'utf-8'

    -- Indicate the desired format/style of Unicode escaping.
    -- Choose JSON-style (JavaScript-style) Unicode escape sequences by using "unicodeescape"
    DECLARE @encoding nvarchar(4000)
    SELECT @encoding = 'unicodeescape'

    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- \ud83e\udde0
    -- \ud83d\udd10
    -- \u2705
    -- \u26a0\ufe0f
    -- \u274c
    -- \u2713
    -- \u4e2d
    -- \u00e9 xyz \u00e0
    -- abc \u79c1 \u306f \u3093 ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    -- Do the same, but use uppercase letters (A-F) in the hex values.
    SELECT @encoding = 'unicodeescape-upper'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- \uD83E\uDDE0
    -- \uD83D\uDD10
    -- \u2705
    -- \u26A0\uFE0F
    -- \u274C
    -- \u2713
    -- \u4E2D
    -- \u00E9 xyz \u00E0
    -- abc \u79C1 \u306F \u3093 ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    --  ECMAScript (JavaScript) �code point escape� syntax

    SELECT @encoding = 'unicodeescape-curly'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- \u{d83e}\u{dde0}
    -- \u{d83d}\u{dd10}
    -- \u{2705}
    -- \u{26a0}\u{fe0f}
    -- \u{274c}
    -- \u{2713}
    -- \u{4e2d}
    -- \u{00e9} xyz \u{00e0}
    -- abc \u{79c1} \u{306f} \u{3093} ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    -- Do the same, but use uppercase letters (A-F) in the hex values.
    SELECT @encoding = 'unicodeescape-curly-upper'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- \u{D83E}\u{DDE0}
    -- \u{D83D}\u{DD10}
    -- \u{2705}
    -- \u{26A0}\u{FE0F}
    -- \u{274C}
    -- \u{2713}
    -- \u{4E2D}
    -- \u{00E9} xyz \u{00E0}
    -- abc \u{79C1} \u{306F} \u{3093} ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    -- HTML hexadecimal character reference

    SELECT @encoding = 'unicodeescape-htmlhex'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- &#x1f9e0;
    -- &#x1f510;
    -- &#x2705;
    -- &#x26a0;&#xfe0f;
    -- &#x274c;
    -- &#x2713;
    -- &#x4e2d;
    -- &#xe9; xyz &#xe0;
    -- abc &#x79c1; &#x306f; &#x3093; ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    -- HTML decimal character reference

    SELECT @encoding = 'unicodeescape-htmldec'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- &#129504;
    -- &#128272;
    -- &#9989;
    -- &#9888;&#65039;
    -- &#10060;
    -- &#10003;
    -- &#20013;
    -- &#233; xyz &#224;
    -- abc &#31169; &#12399; &#12435; ghi

    -- Revert back to the unescaped chars:
    EXEC sp_OAMethod @sb, 'Decode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- -----------------------------------------------------------------------------------------
    -- Unicode code point notation or U+ notation

    SELECT @encoding = 'unicodeescape-plus'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- u+1f9e0
    -- u+1f510
    -- u+2705
    -- u+26a0u+fe0f
    -- u+274c
    -- u+2713
    -- u+4e2d
    -- u+00e9 xyz u+00e0
    -- abc u+79c1 u+306f u+3093 ghi

    -- Chilkat cannot unescape the Unicode code point notation or U+ notation.
    -- For this style, Chilkat only goes in one direction, which is to escape.

    -- To emit uppercase hex, specify unicodeescape-plus-upper
    SELECT @encoding = 'unicodeescape-plus-upper'
    -- ...
    -- ...

    EXEC sp_OAMethod @sb, 'Clear', NULL
    EXEC sp_OAMethod @sb, 'AppendSb', @success OUT, @sbOriginal

    -- -----------------------------------------------------------------------------------------
    -- Hex in Angle Brackets

    SELECT @encoding = 'unicodeescape-angle'
    EXEC sp_OAMethod @sb, 'Encode', @success OUT, @encoding, @charsetNotUsed
    EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0

    -- Output:
    -- <1f9e0>
    -- <1f510>
    -- <2705>
    -- <26a0><fe0f>
    -- <274c>
    -- <2713>
    -- <4e2d>
    -- <e9> xyz <e0>
    -- abc <79c1> <306f> <3093> ghi

    -- Chilkat cannot unescape the angle bracket notation.
    -- For this style, Chilkat only goes in one direction, which is to escape.

    EXEC sp_OAMethod @sb, 'Clear', NULL
    EXEC sp_OAMethod @sb, 'AppendSb', @success OUT, @sbOriginal

    EXEC @hr = sp_OADestroy @sbOriginal
    EXEC @hr = sp_OADestroy @sb


END
GO

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.