Determine the encoding format of uploaded txt files (Text garbled solution), and txt encoding format

Source: Internet
Author: User

Determine the encoding format of uploaded txt files (Text garbled solution), and txt encoding format

Description

  You can use ajax or browse to upload a text file. When uploading a text file, c # may encounter Chinese garbled characters due to different text formats.

Solution

Determine the file encoding format by uploading the file stream, and obtain the text content in the corresponding encoding format.

  # Region checks the encoding type of a given file stream
/// <Summary>
/// Determine the encoding type of a given file stream
/// </Summary>
/// <Param name = "fs"> file stream </param>
/// <Returns> file encoding type </returns>
Private string GetType (byte [] fs, out string errorMsg)
{
String reVal = "Default ";
ErrorMsg = "";
If (IsUTF8Bytes (fs, out errorMsg) | (fs [0] = 0xEF & fs [1] = 0xBB & fs [2] = 0xBF ))
{
ReVal = "UTF8 ";
}
Else if (fs [0] = 0xFE & fs [1] = 0xFF & fs [2] = 0x00)
{
ReVal = "BigEndianUnicode ";
}
Else if (fs [0] = 0xFF & fs [1] = 0xFE & fs [2] = 0x41)
{
ReVal = "Unicode ";
}
Return reVal;

}
# Endregion

// Obtain text content

System. Text. Encoding. Default. GetString (fs)

Test Results

  In Windows, most of Default (Default format), UTF8, BigEndianUnicode, and Unicode can be recognized

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.