I get a ISO-8859-1-encoded string from an exernal DLL, and I must use it to create a link.
Apparently, in production (I have no access to the production server whatsoever), it adds “%00” at the end of the utf-8-decoded strings. I understand that it is a null-terminated string, then. How can I handle this situation ?
My conversion function is this one :
Private Function DecodeString(ByRef SourceData As String, ByVal SourceEncoding As Encoding, ByVal OutputEncoding As Encoding) As String
Dim bSourceData As Byte() = SourceEncoding.GetBytes(SourceData)
Dim bOutData As Byte() = System.Text.Encoding.Convert(SourceEncoding, OutputEncoding, bSourceData)
Return OutputEncoding.GetString(bOutData)
End Function
What do I have to change to make it work ? I thought of something like .Replace("%00", "") but even if it worked, I don’t think it would solve the underlying problem…
I found this : Fastest way to convert a possibly-null-terminated ascii byte[] to a string? but I don’t want to do it “in the fastest way possible”, and I don’t like the “unsafe” part, as it is for a banking site (even if I don’t understand exactly what it can lead to, I don’t want to take any unnecessary risk).
Thanks
No, if you’ve been given a string, it’s already a sequence of Unicode characters (hand-waving around surrogate pairs aside). The fact that it may have originally been loaded from ISO-8859-1 binary data is irrelevant.
Your method is fundamentally flawed. Assuming every character in the string can be represented in both encodings, it should be a no-op. It’s conceptually converting text to bytes in the source encoding, then converting those bytes to text, then converting that text to bytes in the target encoding, then converting those bytes to text. At best that’s a no-op; at worst it could lose data if either encoding can’t handle all the characters in the original string.
You almost certainly need to step up a bit, to where the string is originally being constructed – that’s where the problem will be. At that point, you should probably be able to remove your method entirely.