VBNET function to convert charset encoding to windows-1256
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: VBNET function to convert charset encoding to windows-1256

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    1

    VBNET function to convert charset encoding to windows-1256

    am looking for simple vb.net function to convert text to windows-1256.

    For example the below text :


    when I try to convert it to proper Arabic language I used the online tool http://iosart.com/tools/charset-fixer then choosed Arabic(windows-1256).
    I used the below function with no use :

    Code:
    Public Function ConvertUTF8StringEncoding(ByVal StringToConvert As String, Optional ByVal TargetCharSet As String = "windows-1256") As Byte()
    Dim ByteConvertedString() As Byte
    Dim ByteStringToConvert() As Byte
    Dim TargetEncoding As Encoding
    ' Convert the string to a Byte array
    ByteStringToConvert = Encoding.UTF8.GetBytes(StringToConvert.ToCharArray)
    ' Get the target encoding type
    TargetEncoding = Encoding.GetEncoding(TargetCharSet)
    ' Convert the byte array using the target encoding
    ByteConvertedString = Encoding.Convert(System.Text.Encoding.UTF8, TargetEncoding, ByteStringToConvert)
    Return ByteConvertedString
    End Function
    
    Private Function BinaryUTF8ToString(ByVal Binary() As Byte) As String
    Return System.Text.UnicodeEncoding.UTF8.GetString(Binary)
    End Function
    
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim stringConvertedString As String = BinaryUTF8ToString(ConvertUTF8StringEncoding("  "))
    
    lbresult.Text = stringConvertedString
    End Sub
    The result was : ???E C????? and it suppose to be : كلمة المركز
    Last edited by HanneSThEGreaT; November 18th, 2013 at 07:13 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: VBNET function to convert charset encoding to windows-1256

    I think you are over-complicating things.

    This is all you need :

    Code:
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim strInput As String = " "
    
            Dim btCharBytes As Byte() = Encoding.Default.GetBytes(strInput)
    
            Dim strOutput As New String(Encoding.GetEncoding(1256).GetChars(btCharBytes))
    
            lbResult.Text = strOutput
        End Sub
    Oh, and please remember to use CODE tags when posting code. It is explained here :

    http://forums.codeguru.com/showthrea...for-more-info)
    Last edited by HanneSThEGreaT; November 18th, 2013 at 07:12 AM.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: VBNET function to convert charset encoding to windows-1256

    I forgot one little small detail, that you might also have set already.
    Also remember to set your RightToLeft Property for the TextBox or Label to True

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center