Results 1 to 6 of 6

Thread: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2015
    Posts
    226

    Lightbulb URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

    After searching this forum and the internet for a few days and did not get what I am looking for I stumbled upon this and tweaked it a little bit because it had a problem with encoding vbCrLf.

    It all started when I tried to add SMS capability to an old VB6 application using ClickaTell service but unfortunately it only uses CURL or JavaScript!
    Sending Unicode SMS from VB6 app wasn't possible till I found this and I thought I'd share as it may come handy to others.

    Code:
    Private Declare Sub CopyToMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
    Public Function URLEncode_UTF8( _
          ByVal Text As String _
       ) As String
     
       Dim Index1 As Long
       Dim Index2 As Long
       Dim Result As String
       Dim Chars() As Byte
       Dim Char As String
       Dim Byte1 As Byte
       Dim Byte2 As Byte
       Dim UTF16 As Long
     
       For Index1 = 1 To Len(Text)
          CopyToMemory Byte1, ByVal StrPtr(Text) + ((Index1 - 1) * 2), 1
          CopyToMemory Byte2, ByVal StrPtr(Text) + ((Index1 - 1) * 2) + 1, 1
     
          UTF16 = Byte2
          UTF16 = UTF16 * 256 + Byte1
          Chars = GetUTF8FromUTF16(UTF16)
          For Index2 = LBound(Chars) To UBound(Chars)
             Char = Chr(Chars(Index2))
             If Char Like "[0-9A-Za-z]" Then
                Result = Result & Char
             Else
                If Asc(Char) < 16 Then
                    Result = Result & "%0" & Hex(Asc(Char))
                Else
                    Result = Result & "%" & Hex(Asc(Char))
                End If
             End If
          Next
       Next
       
       URLEncode_UTF8 = Result
       
    End Function
     
    Private Function GetUTF8FromUTF16( _
          ByVal UTF16 As Long _
       ) As Byte()
     
       Dim Result() As Byte
       If UTF16 < &H80 Then
          ReDim Result(0 To 0)
          Result(0) = UTF16
       ElseIf UTF16 < &H800 Then
          ReDim Result(0 To 1)
          Result(1) = &H80 + (UTF16 And &H3F)
          UTF16 = UTF16 \ &H40
          Result(0) = &HC0 + (UTF16 And &H1F)
       Else
          ReDim Result(0 To 2)
          Result(2) = &H80 + (UTF16 And &H3F)
          UTF16 = UTF16 \ &H40
          Result(1) = &H80 + (UTF16 And &H3F)
          UTF16 = UTF16 \ &H40
          Result(0) = &HE0 + (UTF16 And &HF)
       End If
       GetUTF8FromUTF16 = Result
    End Function

    VB6 code to use CURL is as follow after adding a reference to Microsoft Internet Controls :

    Code:
    Inet1.Execute "https://platform.clickatell.com/messages/http/send?apiKey=YourKey&to=MobileNo&content=" & URLEncode_UTF8(YourMessage)
    Enjoy!
    Last edited by labmany; Mar 3rd, 2018 at 03:11 AM.

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,229

    Re: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2015
    Posts
    226

    Re: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

    I did not claim it is mine...but anyway thanks for the link!

  4. #4
    Lively Member vbLewis's Avatar
    Join Date
    Feb 2009
    Location
    USA
    Posts
    127

    Re: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

    This might interest you. I coded up One for my own use...
    As you can see, a small difference in speed and only pure vb...

    Name:  ss.gif
Views: 1166
Size:  16.8 KB
    Attached Files Attached Files

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2015
    Posts
    226

    Re: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

    Quote Originally Posted by vbLewis View Post
    This might interest you. I coded up One for my own use...
    As you can see, a small difference in speed and only pure vb...

    Name:  ss.gif
Views: 1166
Size:  16.8 KB
    Cool, I will test it right away.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2015
    Posts
    226

    Re: URLEncode in UTF-8 with Visual Basic 6 (Sending Unicode SMS message)

    Good work vbLewis.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width