C# .NET - Get file encoding - Asked By Aldo Liaks on 10-Feb-09 02:50 PM

Hi guys,

Is there any way to know the encoding of the file I want to open? (pdf).

Thanks.

System.Text.Encoding - Panji Tengkorak replied to Aldo Liaks on 10-Feb-09 04:45 PM

This is not a C#, it's VB.NET but I am sure you know how to convert it:

Private Function GetFileEncoding(ByVal filePath As String) As String
        Dim enc As System.Text.Encoding = Nothing
        Dim file As System.IO.FileStream = New System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)

        If file.CanSeek Then
            Dim bom As Byte() = New Byte(3) {}
            file.Read(bom, 0, 4)
            If (bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF) OrElse (bom(0) = &HFF AndAlso bom(1) = &HFE) OrElse (bom(0) = &HFE AndAlso bom(1) = &HFF) OrElse (bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF) Then
                enc = System.Text.Encoding.Unicode
            Else
                enc = System.Text.Encoding.ASCII
            End If
            file.Seek(0, System.IO.SeekOrigin.Begin)
        Else
            enc = System.Text.Encoding.ASCII
        End If
        GetFileEncoding = enc.ToString
        file.Close()
    End Function
__________________

Daniel

use File.ReadAllBytes - Venkat K replied to Aldo Liaks on 10-Feb-09 11:56 PM

to load the PDF file, and then encode the byte array as normal using http://msdn.microsoft.com/en-us/library/system.convert.tobase64string.aspx.

Function for encoding:

public string base64Encode(string data)
{
    try
    {
        byte[] encData_byte = new byte[data.Length];
        encData_byte = System.Text.Encoding.UTF8.GetBytes(data);   
        string encodedData = Convert.ToBase64String(encData_byte);
        return encodedData;
    }
    catch(Exception e)
    {
        throw new Exception("Error in base64Encode" + e.Message);
    }
}
File Decoding:
public string base64Decode(string data)
{
    try
    {
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
        System.Text.Decoder utf8Decode = encoder.GetDecoder();
   
        byte[] todecode_byte = Convert.FromBase64String(data);
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);   
        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);                  
        string result = new String(decoded_char);
        return result;
    }
    catch(Exception e)
    {
        throw new Exception("Error in base64Decode" + e.Message);
    }
}

Thanks - Aldo Liaks replied to Panji Tengkorak on 11-Feb-09 01:20 AM

end of post