code page problem

D

David Scemama

Hi,

I'm developing an application that reads data from a file. The data has been
encoded using code page 850.
I use the FileOpen function to open the file and the FileGet function to
read the records, but the string I read is encoded using code page 1252.

Is there a way to specify the codepage before reading the data, or do I have
to convert every string I find ?
I've found a sample of convertion function in the help, but it slows down my
application:

Protected Function ConvertString(ByVal str As String) As String
Dim enc As System.text.Encoding =
System.Text.Encoding.GetEncoding(850)
Dim encw As System.text.Encoding =
System.Text.Encoding.GetEncoding(1252)

' Convert the string into a byte[].
Dim wBytes As Byte() = encw.GetBytes(str)

' Perform the conversion from one encoding to the other.
Dim bytes As Byte() = System.Text.Encoding.Convert(enc, encw,
wBytes)

' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(encw.GetCharCount(bytes, 0, bytes.Length)) As Char
encw.GetChars(bytes, 0, bytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

Return asciiString
End Function

Thanks for your help

David
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi David,

You can use StreamReader which allow for specifying the source encoding upon
instance construction.
 
D

David Scemama

I'm using FileGet because I'm reading records and using it, I directly get
the records filled after reading.
Is there a way to do the same using streamreader ?
David

Dmitriy Lapshin said:
Hi David,

You can use StreamReader which allow for specifying the source encoding upon
instance construction.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

David Scemama said:
Hi,

I'm developing an application that reads data from a file. The data has been
encoded using code page 850.
I use the FileOpen function to open the file and the FileGet function to
read the records, but the string I read is encoded using code page 1252.

Is there a way to specify the codepage before reading the data, or do I have
to convert every string I find ?
I've found a sample of convertion function in the help, but it slows
down
my
application:

Protected Function ConvertString(ByVal str As String) As String
Dim enc As System.text.Encoding =
System.Text.Encoding.GetEncoding(850)
Dim encw As System.text.Encoding =
System.Text.Encoding.GetEncoding(1252)

' Convert the string into a byte[].
Dim wBytes As Byte() = encw.GetBytes(str)

' Perform the conversion from one encoding to the other.
Dim bytes As Byte() = System.Text.Encoding.Convert(enc, encw,
wBytes)

' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(encw.GetCharCount(bytes, 0, bytes.Length)) As Char
encw.GetChars(bytes, 0, bytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

Return asciiString
End Function

Thanks for your help

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top