bmccleary
asked on
Convert Unicode String to ASCII String
I have a simple problem that i need a simple answer for. I have a string that is in unicode format that I want to convert to ASCII format. If the character is not in the ASCII range, then it just needs to be dropped. I need this function in ASP (VBScript), so I can't use any fancy C++ function. For example, I have the following code:
-------------------------- ---------- ---------- -------
Dim sUnicodeValue, sAsciiValue
sUnicodeValue = "Ÿ"
sAsciiValue = ConvertUnicodeStringToAsci i(sUnicode Value) ' This function is what I need
Response.Write "Unicode = " & sUnicodeValue & "<br>"
Response.Write "ASCII = " & sAsciiValue & "<br>"
-------------------------- ---------- ---------- -------
How can I get the above to work? Any help is appreciated...
--------------------------
Dim sUnicodeValue, sAsciiValue
sUnicodeValue = "Ÿ"
sAsciiValue = ConvertUnicodeStringToAsci
Response.Write "Unicode = " & sUnicodeValue & "<br>"
Response.Write "ASCII = " & sAsciiValue & "<br>"
--------------------------
How can I get the above to work? Any help is appreciated...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is your final code
<%
Function UnicodeToAscii(ByRef pstrUnicode)
Dim llngLength
Dim llngIndex
Dim llngAscii
Dim lstrAscii
llngLength = Len(pstrUnicode)
For llngIndex = 1 To llngLength
llngAscii = Asc(Mid(pstrUnicode, llngIndex, 1))
lstrAscii = lstrUnicode & ChrB(llngAscii)
Next
UnicodeToAscii = lstrAscii
End Function
Function AsciiToUnicode(ByRef pstrAscii)
Dim llngLength
Dim llngIndex
Dim llngAscii
Dim lstrUnicode
llngLength = LenB(pstrAscii)
For llngIndex = 1 To llngLength
llngAscii = AscB(MidB(pstrAscii, llngIndex, 1))
lstrUnicode = lstrUnicode & Chr(llngAscii)
Next
AsciiToUnicode = lstrUnicode
End Function
Dim sUnicodeValue, sAsciiValue
sUnicodeValue = "Ÿ"
sAsciiValue = UnicodeToAscii(sUnicodeVal ue) ' This function is what I need
Response.Write "Unicode = " & sUnicodeValue & "<br>"
Response.Write "ASCII = " & sAsciiValue & "<br>"
Response.Write "<br>Unicode = " & AsciiToUnicode(sAsciiValue ) & "<br>"
%>
<%
Function UnicodeToAscii(ByRef pstrUnicode)
Dim llngLength
Dim llngIndex
Dim llngAscii
Dim lstrAscii
llngLength = Len(pstrUnicode)
For llngIndex = 1 To llngLength
llngAscii = Asc(Mid(pstrUnicode, llngIndex, 1))
lstrAscii = lstrUnicode & ChrB(llngAscii)
Next
UnicodeToAscii = lstrAscii
End Function
Function AsciiToUnicode(ByRef pstrAscii)
Dim llngLength
Dim llngIndex
Dim llngAscii
Dim lstrUnicode
llngLength = LenB(pstrAscii)
For llngIndex = 1 To llngLength
llngAscii = AscB(MidB(pstrAscii, llngIndex, 1))
lstrUnicode = lstrUnicode & Chr(llngAscii)
Next
AsciiToUnicode = lstrUnicode
End Function
Dim sUnicodeValue, sAsciiValue
sUnicodeValue = "Ÿ"
sAsciiValue = UnicodeToAscii(sUnicodeVal
Response.Write "Unicode = " & sUnicodeValue & "<br>"
Response.Write "ASCII = " & sAsciiValue & "<br>"
Response.Write "<br>Unicode = " & AsciiToUnicode(sAsciiValue
%>
StrConv("Ÿ", vbFromUnicode) - to convert from Unicode to system default codepage
StrConv("ascii", vbUnicode) - converts to Unicode
StrConv("ascii", vbUnicode) - converts to Unicode
*asy=Easy
Oops,
I haven't refreshed. You are quite fast
Oops,
I haven't refreshed. You are quite fast
ASKER
Thanks guys, but I have two problems...
First, StrConv is not available as a function in VBScript so I can't use it in my ASP pages
Secondly, Sudhaker, I modified you code slightly so that I only get the standard (not even extended) ASCII character set, as follows:
Function ConvertToASCII(sValue)
Dim i, rv, charValue
rv = ""
For i = 1 To Len(sValue)
charValue = Asc(Mid(sValue, i, 1))
If charValue <= 127 Then
rv = rv & Chr(charValue)
End If
Next
ConvertToStandardASCII = rv
End Function
THe problem is that some of my strings are very long (1MB+), and using this function causes severe processor loading and memory utilization (because of the continual appending of the string). I know that I can use a complex VB string builder method, but I was hoping for a simpler method.
Do either of you know a simpler method to make my function work, or how to get StrConv to work with VBScript?
First, StrConv is not available as a function in VBScript so I can't use it in my ASP pages
Secondly, Sudhaker, I modified you code slightly so that I only get the standard (not even extended) ASCII character set, as follows:
Function ConvertToASCII(sValue)
Dim i, rv, charValue
rv = ""
For i = 1 To Len(sValue)
charValue = Asc(Mid(sValue, i, 1))
If charValue <= 127 Then
rv = rv & Chr(charValue)
End If
Next
ConvertToStandardASCII = rv
End Function
THe problem is that some of my strings are very long (1MB+), and using this function causes severe processor loading and memory utilization (because of the continual appending of the string). I know that I can use a complex VB string builder method, but I was hoping for a simpler method.
Do either of you know a simpler method to make my function work, or how to get StrConv to work with VBScript?
I assume that more or less both will have same speed performance. Maybe strconv is a bit faster than user defined function.
Coming to the strconv in asp pages, I think it is there with VBScript engine. Although I am not able to test the function at my side because of Type Mismatch error, I strongly say it is there. May be some body can help u on this
Regards
Sudhakar
Coming to the strconv in asp pages, I think it is there with VBScript engine. Although I am not able to test the function at my side because of Type Mismatch error, I strongly say it is there. May be some body can help u on this
Regards
Sudhakar
Since the StrConv is not available in VBScript, consider writing a VB dll with this function that you use from ASP. Assuming it is fast enough, that is.
>> Since the StrConv is not available in VBScript, consider writing a VB dll with this function that you use from ASP. Assuming it is fast enough, that is.
I thought the same. But as this is web based project, the hosting people may not accept such components, I think.
I thought the same. But as this is web based project, the hosting people may not accept such components, I think.
ASKER
Thanks to you all for your inputs. I was hoping for either something cleaner or quicker, but I guess this is one example of why classic ASP is going the way of the dodo! Thanks again.
I bumped into this (by way of google) while investigating how to make fast strings, so I will put in a way that should help the speed of the processing, even though it is closed, feeling generous.
I am more familiar with VB Classic so I hope this works for you without any changes
Function ConvertToASCII(sValue)
Dim i, lStringLength, sReturnValue, iBufferSize, lCurStringSize, charValue
lStringSize= len(svalue)
iBufferSize = 15000
lCurStringSize = 0
For i = 1 To lStringLength
If i > iCurStringSize Then
iCurStringSize = iCurStringSize + iBufferSize
sReturnValue = sReturnValue & String$(iBufferSize, 0)
End if
charValue = AscB$(MidB$(sValue, i)) ' notice I am not specifiying the length, this makes it
' faster and only the first charcter is taken anyway
Mid$(sReturnValue, lCurStringSize + 1, 1) = Chr$(charValue)
Next
End Function
I am more familiar with VB Classic so I hope this works for you without any changes
Function ConvertToASCII(sValue)
Dim i, lStringLength, sReturnValue, iBufferSize, lCurStringSize, charValue
lStringSize= len(svalue)
iBufferSize = 15000
lCurStringSize = 0
For i = 1 To lStringLength
If i > iCurStringSize Then
iCurStringSize = iCurStringSize + iBufferSize
sReturnValue = sReturnValue & String$(iBufferSize, 0)
End if
charValue = AscB$(MidB$(sValue, i)) ' notice I am not specifiying the length, this makes it
' faster and only the first charcter is taken anyway
Mid$(sReturnValue, lCurStringSize + 1, 1) = Chr$(charValue)
Next
End Function
Sudhakar you are a SUPERSTAR! THANK YOU THANK YOU
Function UnicodeToAscii(ByRef pstrUnicode)
Dim llngLength
Dim llngIndex
Dim llngAscii
Dim lstrAscii
llngLength = Len(pstrUnicode)
For llngIndex = 1 To llngLength
llngAscii = Asc(Mid(pstrUnicode, llngIndex, 1))
lstrAscii = lstrUnicode & ChrB(llngAscii)
Next
UnicodeToAscii = lstrAscii
End Function
Regards
Sudhakar