URL decoding Cyrillic (etc) characters

katerina

Registered User.
Local time
Today, 20:26
Joined
Aug 30, 2011
Messages
11
Hi All,

Am having trouble decoding URL-safe Cyrillic characters back to Cyrillic.

e.g.
Code:
%D0%A2%D1%8E%D0%BC%D0%B5%D0%BD%D1%81%D0%BA%D0%B0%D1%8F%20%D0%BE%D0%B1%D0%BB%D0%B0%D1%81%D1%82%D1%8C
is
Тюменская область

The javascript fuction decodeURIComponent works fine (check the URL decoder on meyerweb)

however I can't manage to find a VBA equivilent. The following works for your standard A-Z, but not for the above.

Any ideas?

Many thanks
Katerina

Code:
Public Function URLDecode(StringToDecode As String) As String

Dim TempAns As String
Dim CurChr As Integer

CurChr = 1

Do Until CurChr - 1 = Len(StringToDecode)
  Select Case Mid(StringToDecode, CurChr, 1)
    Case "+"
      TempAns = TempAns & " "
    Case "%"
      TempAns = TempAns & Chr(Val("&h" & _
         Mid(StringToDecode, CurChr + 1, 2)))
       CurChr = CurChr + 2
    Case Else
      TempAns = TempAns & Mid(StringToDecode, CurChr, 1)
  End Select

CurChr = CurChr + 1
Loop

URLDecode = TempAns
End Function
 
Last edited:
Hi,

Thanks for the response. I know it doesn't like it - I'm hopeful I can pass it through OK - for example I can send Cyrillic from a table out to a unicode msg box fine:

Code:
Private Declare Function MessageBox _        Lib "User32" Alias "MessageBoxW" _
            (ByVal hWnd As Long, _
             ByVal lpText As Long, _
             ByVal lpCaption As Long, _
             ByVal wType As Long) _
         As Long
   Function DisplayUnicodeMessage(Msg As String)
   
  Dim Caption As String
  Caption = "My Unicode Text" ' or whatever
   
  MessageBox &H0&, StrPtr(Msg), StrPtr(Caption), vbOKOnly
   
  End Function

Sending 'Msg' as a DLookup straight from the table is fine. Looking at the value in the immediate or whatever it's junk. Printing on forms and tables and reports is fine.
 
Hi all,

I think the problem lies in the HTML-encoded text I'm receiving. For example, the letter Я has a character code of 1071 and a hex code of 42F.

Changing Chr to ChrW allows greater than the standard 0-255 Chr range (except on Mac).

So both of

Code:
?DisplayUnicodeMessage(chrw(1071))
?DisplayUnicodeMessage(chrw(val("&h42F")))
(using my above-posted dec) display the letter Я correctly.

So, my question is - how to get from the HTML coding of %D1%8F to the hex value of 42F for the letter Я?

And/or, is this any way to call the Javascript function decodeURIComponent from VBA because that would solve the problem.

Any ideas?

TIA
 
Last edited:
...the letter Я has a character code of 1071 and a hex code of 42F.

To be clear, that's what the codes are in VBA.

Both the encoded text, and the javascript functions uncode/decodeURIComponent have it as %D1%8F
 
Last edited:
To be clear, that's what the codes are in VBA.

Both the encoded text, and the javascript functions uncode/decodeURIComponent have it as %D1%8F

You can use an ADO Stream object to convert between UTF-8 encoding and unicode.

Code:
Sub StreamTest()
Dim ad As ADODB.Stream
Set ad = New ADODB.Stream
ad.Open
ad.Charset = "utf-8"
ad.Position = 0
ad.Type = adTypeBinary
Dim b(1 To 2) As Byte
b(1) = 209 'D1
b(2) = 143 '8F
ad.Write b

ad.Position = 0
ad.Type = adTypeText
DisplayUnicodeMessage ad.ReadText(adReadAll)
ad.Close
End Sub
 

Users who are viewing this thread

Back
Top Bottom