locked
How I Can Save In Ascii Encoding Using CodePage 1256

    Question

  • I need to write some information to a text File using ascii encoding and code page 1256 windows-arabic bcz my string conatins both arabic and english chars I try to use Encoding.Ascii to do the conversion but the code page used is 20127 so the arabic chars is saved to file as ????????? and I coludn't change the code page to my desired one

    how I can save the information to the text file using ascii encoding with the correct code page
    • Changed type Samer Selo Wednesday, January 06, 2010 12:41 PM it's question
    Wednesday, January 06, 2010 12:39 PM

Answers

  • thank you

    Problem Solved In a simple way



     

    My.Computer.FileSystem.WriteAllText(M3uFileName, _

    TMPSTR, _

     True, Encoding.GetEncoding(1256))

    • Marked as answer by Samer Selo Wednesday, January 06, 2010 7:15 PM
    Wednesday, January 06, 2010 7:12 PM

All replies

  • Hi,

    You can do something like this:

    ''' Code posted by Xun Ye [http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e17f99a9-f4fa-49a6-82da-0c73d930224d/]
    Class Program
        Private Shared Sub Main(ByVal args As String())
            ConvertFile(28596, 1256, "F:\sourceFile.txt", "F:\destFile.txt")
        End Sub
        
        '=28596
        '=1256
        Private Shared Sub ConvertFile(ByVal fromEncode As Integer, ByVal toEncode As Integer, ByVal sourceFilePath As String, ByVal DestFilePath As String)
            Dim fs As New FileStream(sourceFilePath, FileMode.Open)
            Dim br As New BinaryReader(fs)
            
            Dim orignalEncode As Byte() = br.ReadBytes(CInt(fs.Length))
            
            br.Close()
            fs.Close()
            
            Dim unEncodedText As String = Encoding.GetEncoding(fromEncode).GetString(orignalEncode)
            
            fs = New FileStream(DestFilePath, FileMode.Create)
            Dim bw As New BinaryWriter(fs)
            
            Dim encodedBuffer As Byte() = Encoding.GetEncoding(fromEncode).GetBytes(unEncodedText)
            
            bw.Write(encodedBuffer, 0, encodedBuffer.Length)
            bw.Flush()
            bw.Close()
        End Sub
    End Class
    I've changed it, sorry, this is definetly one of those days which I should be more aware, I'm sorry.

    Hope it helps.

    Regards.
    -

    Esteban Murchio.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Blog
    • Proposed as answer by Esteban M Wednesday, January 06, 2010 1:02 PM
    • Unproposed as answer by Samer Selo Wednesday, January 06, 2010 6:08 PM
    • Edited by Esteban M Wednesday, January 06, 2010 6:11 PM Changed code language
    Wednesday, January 06, 2010 1:01 PM
  • But that's C........
    Renee
    Wednesday, January 06, 2010 4:12 PM
  • Thank You
    The Code in C and it's wrong the toEncode is not used inside the procedure

    I want to save a string variable wich have unicode format by default to ascii text file using code page 1256 the process of saving the string is used to create the file
    • Edited by Samer Selo Wednesday, January 06, 2010 6:12 PM
    Wednesday, January 06, 2010 6:05 PM
  • Hi,

    I'm sorry, I've changed it.

    Regards,
    Esteban.


    Blog
    Wednesday, January 06, 2010 6:11 PM
  • thank you

    Problem Solved In a simple way



     

    My.Computer.FileSystem.WriteAllText(M3uFileName, _

    TMPSTR, _

     True, Encoding.GetEncoding(1256))

    • Marked as answer by Samer Selo Wednesday, January 06, 2010 7:15 PM
    Wednesday, January 06, 2010 7:12 PM