CPConvert() Method



|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.

[pic]

[Visual Basic]

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

[Visual Basic]

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

[Visual Basic]

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: mon

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family

Assembly: VFPToolkit (in VFPToolkitNET.dll)

See Also

mon Members | VFPToolkit Namespace

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download