Visual FoxPro Toolkit for .NET

CPConvert() Method

Converts a string in one CodePage to another. This method receives three parameters; a string which is to be converted, the current code page number and the new code page number.

[VisualBasic]

Public Shared Function CPConvert(ByVal nCurrentCodePage As Integer, ByVal nNewCodePage As Integer, ByVal cExpression As String) As String

[C#]

public static string CPConvert(int nCurrentCodePage, int nNewCodePage, string cExpression)

Example

[VisualBasic]

This example converts the string from Windows CP to Mac CP

Dim lcString1 As String = "Kämäl"

Dim lcString2 As String

lcString2 = CPConvert(1252, 10000, lcString1) 'Returns "K?m?l"

Console.WriteLine(lcString2)

Console.WriteLine(lcString1)

[C#]

string MyString = "äää";

VFPToolkit.others.CPConvert(1252, 10000, MyString);//Converts the string from Windows CP to Mac CP

Implementation

[VisualBasic]

Public Shared Function CPConvert(ByVal nCurrentCodePage As Integer, ByVal nNewCodePage As Integer, ByVal cExpression As String) As String
Dim i As Integer = 0
Dim nLength As Integer = cExpression.Length
'Create a current and new array of bytes with the length of the string
Dim aCurr() As Byte = New Byte(nLength) {}
Dim aNew() As Byte = New Byte(nLength) {}
'Fill the current array from the string
For i = 0 To cExpression.Length - 1 Step i + 1
aCurr(i) = Convert.ToByte(cExpression.Chars(i))
Next
'Get the encoding objects for the current and new Code Pages
Dim CurCP As Encoding = Encoding.GetEncoding(nCurrentCodePage)
Dim NewCP As Encoding = Encoding.GetEncoding(nNewCodePage)
'Fill the new array after converting current code page to new code page
aNew = Encoding.Convert(CurCP, NewCP, aCurr)
'We still have bytes so we convert each byte to a char and add it to a string builder
Dim sb As StringBuilder = New StringBuilder()
For i = 0 To cExpression.Length - 1 Step i + 1
sb.Append(Convert.ToChar(aNew(i)))
Next
'Return a string back
Return sb.ToString()
End Function

[C#]

public static string CPConvert(int nCurrentCodePage, int nNewCodePage, string cExpression)
{
int i=0;
int nLength = cExpression.Length;
//Create a current and new array of bytes with the length of the string
byte[] aCurr = new byte[nLength];
byte[] aNew = new byte[nLength];
//Fill the current array from the string
for (i=0; i< cExpression.Length; i++)
{
aCurr[i] = Convert.ToByte(cExpression[i]);
}
//Get the encoding objects for the current and new Code Pages
Encoding CurCP = Encoding.GetEncoding(nCurrentCodePage);
Encoding NewCP = Encoding.GetEncoding(nNewCodePage);
//Fill the new array after converting current code page to new code page
aNew = Encoding.Convert(CurCP, NewCP, aCurr);
//We still have bytes so we convert each byte to a char and add it to a string builder
StringBuilder sb = new StringBuilder();
for (i=0; i< cExpression.Length; i++)
{
sb.Append(Convert.ToChar(aNew[i]));
}
//Return a string back
return sb.ToString();
}

Requirements

Namespace:VFPToolkit

Class:VFPToolkit.common

Platforms:Windows98, WindowsNT4.0, WindowsMillenniumEdition, Windows2000, WindowsXPHomeEdition, WindowsXPProfessional, Windows.NETServerfamily

Assembly:VFPToolkit (in VFPToolkitNET.dll)

See Also

VFPToolkit.common Members | VFPToolkit Namespace