Byte stream coding is so complicated, but it is also very simple

xiaoxiao2021-03-06  41

A) There are many cases in which we need to know the code. Knowing its encoding 3) Read the source code for a web page and you want to output the correct code to output the correct content.

2) Discussing the initial and colleagues to discuss how to obtain file / stream / bytes is UTF8 encoding, found that C # does not have a direct function, but found that the beginning of UTF8 in the SDK is "0xef 0xbb 0xbf" (called Bom - BYTE ORDER MARK, the beginning of the unicode file is "0xff 0xfe", the beginning of the UTF16SMALLENDIAN file is "0xfe 0xFF", so I wrote a simple function to solve it, because there is no time At sufficient test, I always feel that there is a problem ...

/ ** /

///

/// Analysis The BYTE array is the encoding /// /// The encoding type to be transmitted /// The BYTE array to be parsed /// The following example demonstrates how to use method /// /// if (Request.Files.count! = 0) /// {/// // Get uploaded file /// httppostedfile pf = request.files [0]; /// int filelength = (int) PF.InputStream.length ; // // Store the byte array of uploaded files /// BYTE [] BUFF = New Byte [fileLength]; /// pf.inputstream.read (buff, 0, filength); /// pf.inputstream. Close (); /// encoding enc = null; /// getencoding (Out ENC, BUFF); /// Response.write ("encoded: enc.EncodingName); /// Response.write ("
"); /// Response.write (" Content: " Enc.getstring (buff)); ///} /// ///

Void

GetENCODING

OUT

Encoding ENC,

Byte

[] BUFF)

{Bool flag = false; // Used to test the encoding byte [] TESTENCBUFF = New Byte [0]; int filelength = buff.length; // Determine whether the encoded of the uploaded file is Unicode Enc = Encoding.Unicode; TESTENCBUFF = Enc.getPreamble (); if (FileLength> TestencBuff [0] == BUFF [0] && TestencBuff [1] == BUFF [1]) {flag = true;} // Judgment uploaded file encoding Whether it is UTF8 if (! Flag) {eNC = encoding.utf8; TESTENCBUFF = Enc.getPreamble (); if (FileLength> TestencBuff.Length && TestencBuff [0] == BUFF [0] && testENCBUFF [1] == buff [ 1] && TestencBuff [2] == BUFF [2]) {FLAG = true;}} // Determine whether the encoding of uploaded files is BiGendianunicode if (! Flag) {ENC = Encoding.biGendianunicode; TESTENCBUFF = Enc.getPreamble ); If (FileLength> TestencBuff [0] == Buff [0] && testencbuff [1] == buff [1]) {flag = true;}} if (! Flag) {ENC = Encoding.default {ENC = Encoding.default The problem has appeared, the next day colleagues told me that all UTF8 encoded files have BOM information, how to solve it? He first found the answer word stream coding to get so complex (I also searched on Google and Baidu, found that C # does not have a good solution) The following is two related solutions (Java) http: // Dev.9cbs.net/develop/article/10/10961.shtm http://dev.9cbs.net/develop/Article/10/10962.shtm

The Java code is easy to transplant to .NET, then I will come to everyone ... code has more than 2,400 lines, please download code here: Download code

Thank you for your participation in the transplant code: Playyuer

转载请注明原文地址:https://www.9cbs.com/read-54443.html

New Post(0)