468,272 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

code page problem

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
Nov 20 '05 #1
2 1761
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" <da***********@nospam.wanadoo.fr> wrote in message
news:uy*************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #2
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:eI**************@TK2MSFTNGP09.phx.gbl...
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" <da***********@nospam.wanadoo.fr> wrote in message
news:uy*************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by CSDunn | last post: by
10 posts views Thread by Don Munroe | last post: by
171 posts views Thread by tshad | last post: by
1 post views Thread by Bill | last post: by
7 posts views Thread by Mike Rand | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.