Average Joe
Average Joe

Reputation: 4601

Arabic appears gibberish

My users copy and paste arabic text from an arabic newspaper into a textarea. I'd like to be able to store the arabic in terms of char codes such as & # 1500 ; & # 1501; and so on. How do I do that?

When I use the following snippet, I end up getting wrong numbers... First of all, each char I convert to number ends up as 3 digit, whereas I know Arabic char code entities are 4 digits.

IncomingArabic = request("IncomingArabic") 
MaxLen = Len(IncomingArabic)  
For i = 1 To MaxLen
    curChar = Mid(IncomingArabic, lLoop, 1)
    ''# curChar is an arabic char
    iChr = Asc(curChar)  ''# this gives me a 3 digit! And when I tried HEX(curChar) here, it gave a type mismatch error. 

    Encoded = Encoded & "&#" & iChr & ";"
Next
Response.write Encoded ''# shows gibberish! 

Upvotes: 1

Views: 1850

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189457

Here is what I would. Switch everything to use UTF-8. Make sure that the page posting the form is sent with Response.CharSet = "UTF-8" and its Response.CodePage = 65001. Do they same to the receiving page. Now you need not do any mucking about no matter what language is being used.

Upvotes: 1

Average Joe
Average Joe

Reputation: 4601

Well, I sorted out. Just use the Arabize function I placed below.

''# example usage
response.write Arabize(request("IncomingArabic")) //gives you the correct 4 digit sequence!  


Function Arabize(Str)
  Dim Bytes
  dim FromCharset, ToCharset
  FromCharset = "windows-1256"
  ToCharset = "windows-1256"
  Bytes = StringToBytes(Str, FromCharset)
  dim temp
  temp = BytesToString(Bytes, ToCharset)
  Arabize = server.htmlencode(temp)

End Function 

''# you are gonna need the rest too... 
Const adTypeBinary = 1
Const adTypeText = 2

''#  accept a string and convert it to Bytes array in the selected Charset
Function StringToBytes(Str,Charset)
  Dim Stream : Set Stream = Server.CreateObject("ADODB.Stream")
  Stream.Type = adTypeText
  Stream.Charset = Charset
  Stream.Open
  Stream.WriteText Str
  Stream.Flush
  Stream.Position = 0
  ''# rewind stream and read Bytes
  Stream.Type = adTypeBinary
  StringToBytes= Stream.Read
  Stream.Close
  Set Stream = Nothing
End Function

''# accept Bytes array and convert it to a string using the selected charset
Function BytesToString(Bytes, Charset)
  Dim Stream : Set Stream = Server.CreateObject("ADODB.Stream")
  Stream.Charset = Charset
  Stream.Type = adTypeBinary
  Stream.Open
  Stream.Write Bytes
  Stream.Flush
  Stream.Position = 0
  ''# rewind stream and read text
  Stream.Type = adTypeText
  BytesToString= Stream.ReadText
  Stream.Close
  Set Stream = Nothing
End Function

Upvotes: 0

Related Questions