123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace WWPipeLine.Commons
- {
-
-
-
- public class EncodeHelper
- {
- private EncodeHelper()
- {
- }
- #region 判断文件流是否为UTF8字符集
-
-
-
-
-
- public static bool IsUTF8(FileStream sbInputStream)
- {
- int i;
- byte cOctets;
- byte chr;
- bool bAllAscii = true;
- long iLen = sbInputStream.Length;
- cOctets = 0;
- for (i = 0; i < iLen; i++)
- {
- chr = (byte)sbInputStream.ReadByte();
- if ((chr & 0x80) != 0) bAllAscii = false;
- if (cOctets == 0)
- {
- if (chr >= 0x80)
- {
- do
- {
- chr <<= 1;
- cOctets++;
- }
- while ((chr & 0x80) != 0);
- cOctets--;
- if (cOctets == 0) return false;
- }
- }
- else
- {
- if ((chr & 0xC0) != 0x80)
- {
- return false;
- }
- cOctets--;
- }
- }
- if (cOctets > 0)
- {
- return false;
- }
- if (bAllAscii)
- {
- return false;
- }
- return true;
- }
- #endregion 判断文件流是否为UTF8字符集
- #region GB2312转换成UTF-8
-
-
-
-
-
- public static string GB2312ToUTF8(string content)
- {
- byte[] _byteData = GetDefaultByte(content);
- return Encoding.UTF8.GetString(_byteData);
- }
- #endregion GB2312转换成UTF-8
- #region 将字符串转换默认的Byte数组
-
-
-
-
-
- public static byte[] GetDefaultByte(string content)
- {
- return Encoding.Default.GetBytes(content);
- }
- #endregion 将字符串转换默认的Byte数组
- #region 文件转化为Base64
-
-
-
-
-
- public string FileToBase64String(string path)
- {
- FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
- byte[] buffer = new byte[fileStream.Length];
- fileStream.Read(buffer, 0, (int)fileStream.Length);
- fileStream.Close();
- fileStream.Dispose();
- MemoryStream memory = new MemoryStream(buffer);
- string base64 = Convert.ToBase64String(memory.ToArray());
- memory.Close();
- memory.Dispose();
- return base64;
- }
- #endregion 文件转化为Base64
- #region 图象转化为Base64
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion 图象转化为Base64
- #region Base64转化为图象
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion Base64转化为图象
- #region 图标转化为Base64
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion 图标转化为Base64
- #region Base64转化为图标
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion Base64转化为图标
- #region IP 协议采用统一的校验算法
-
-
-
-
-
-
- public static UInt16 checksum(UInt16[] buffer, int size)
- {
- Int32 cksum = 0;
- int counter;
- counter = 0;
- while (size > 0)
- {
- UInt16 val = buffer[counter];
- cksum += Convert.ToInt32(buffer[counter]);
- counter += 1;
- size = -1;
- }
- cksum = (cksum >> 16) + (cksum & 0xffff);
- cksum += (cksum >> 16);
- return (UInt16)(~cksum);
- }
- #endregion IP 协议采用统一的校验算法
- #region 生成字符串的ascii代码
-
-
-
-
-
- public static string Gen_ASCII(string s_Chinese)
- {
- string str = "";
- for (int i = 0; i < s_Chinese.Length; i++)
- {
- char ch = s_Chinese[i];
- if ((ch != '\r') && (ch != '\n'))
- {
- str = str + ((short)ch).ToString("X");
- }
- }
- return str;
- }
-
-
-
-
-
- public static string Get_ASCII(string s_Chinese)
- {
- string str = "";
- byte[] array = Encoding.ASCII.GetBytes(s_Chinese);
- for (int i = 0; i < array.Length; i++)
- {
- int asciicode = (int)(array[i]);
- if (asciicode > 63)
- {
- asciicode -= 64;
- }
- else
- {
- asciicode += 64;
- }
- str += Convert.ToString(asciicode);
- }
- return str;
- }
-
-
-
-
-
- public static string Get_ASCII(byte[] ByteArray)
- {
- ASCIIEncoding AE = new ASCIIEncoding();
- string strAsc = AE.GetString(ByteArray);
- return strAsc;
- }
- #endregion 生成字符串的ascii代码
- #region 生成ascii代码的字符数组
-
-
-
-
-
- public static char[] Gen_ASCII(byte[] ByteArray)
- {
- ASCIIEncoding AE = new ASCIIEncoding();
- char[] CharArray = AE.GetChars(ByteArray);
- return CharArray;
- }
- #endregion 生成ascii代码的字符数组
- #region 字节数组转16进制字符串
-
-
-
-
-
- public static string byteToHexStr(byte[] bytes)
- {
- string returnStr = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- returnStr += bytes[i].ToString("X2") + " ";
- }
- }
- return returnStr;
- }
-
-
-
-
-
- public static string byteToHexStrTrim(byte[] bytes)
- {
- string returnStr = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- returnStr += bytes[i].ToString("X2");
- }
- }
- return returnStr;
- }
- #endregion 字节数组转16进制字符串
- #region 字符串转16进制字节数组
-
-
-
-
-
- public static byte[] strToToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- return returnBytes;
- }
- #endregion 字符串转16进制字节数组
- #region 16进制字符串异或
-
-
-
-
-
- public static string str16Xor(string hexString)
- {
- string returnStr = "";
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte XorByte = 0;
- for (int i = 0; i < hexString.Length / 2; i++)
- XorByte ^= Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- byte[] strAry = Encoding.ASCII.GetBytes(XorByte.ToString("X"));
- returnStr = EncodeHelper.byteToHexStr(strAry);
- return returnStr;
- }
- #endregion 16进制字符串异或
- #region 转全角\半角的函数
-
-
-
-
-
-
-
-
-
-
-
- public static String ToSBC(String input)
- {
-
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- c[i] = (char)(c[i] + 65248);
- }
- return new String(c);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static String ToDBC(String input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new String(c);
- }
- #endregion 转全角\半角的函数
- #region 是否全角\半角
-
-
-
-
-
-
-
-
-
-
-
- public static bool IsSBC(string input)
- {
- if (2 * input.Length == Encoding.Default.GetByteCount(input))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public static bool IsDBC(string input)
- {
- if (input.Length == Encoding.Default.GetByteCount(input))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion 是否全角\半角
- #region 转换Image到byte[] 数组
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion 转换Image到byte[] 数组
- #region 转换byte[]到Image
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion 转换byte[]到Image
- #region 将字节数组转换成字符串
-
-
-
-
-
- public static string byteToString(byte[] b)
- {
- Encoding enc = Encoding.GetEncoding("GB2312");
- string str = enc.GetString(b);
- str = str.Substring(0, str.IndexOf('\0') >= 0 ? str.IndexOf('\0') : str.Length);
- return str;
- }
- #endregion 将字节数组转换成字符串
- #region 编码方式转换
- public static string StringToBitString(string input)
- {
- return StringToBitString(input, Encoding.Default);
- }
- public static string StringToBitString(string input, Encoding encoding)
- {
- byte[] stringBytes = encoding.GetBytes(input);
- StringBuilder result = new StringBuilder(stringBytes.Length * 8);
- foreach (byte b in stringBytes)
- {
- for (int i = 128; i > 0; i /= 2)
- {
- result.Append(((b & i) == 0) ? 0 : 1);
- }
- }
- return result.ToString();
- }
- public static string BitStringToString(string input)
- {
- return BitStringToString(input, Encoding.Default);
- }
- public static string BitStringToString(string input, Encoding encoding)
- {
- List<byte> byteArray = new List<byte>();
- byte temp = 0;
- for (int i = 0, j = 0; i < input.Length; i++)
- {
- if (input[i] == '1') temp |= (byte)(1 << (8 - i % 8 - 1));
- if (++j == 8)
- {
- byteArray.Add(temp);
- temp = 0;
- j = 0;
- }
- }
- string result = new string(encoding.GetChars(byteArray.ToArray()));
- return result;
- }
- #endregion 编码方式转换
- #region int byte 互转
-
-
-
-
-
- public static int BytesToInt(byte[] bytesData)
- {
- int num = 0;
- for (int i = 0; i < bytesData.Length; i++)
- {
- num += bytesData[i] << (8 * i);
- }
- return num;
- }
-
-
-
-
-
-
-
- public static int BytesToInt(byte[] bytesData, int offset, int length = 4)
- {
- int num = 0;
- if (length < 1 || length > 4)
- {
- length = 2;
- }
- for (int i = 0; i < length; i++)
- {
- num += bytesData[i + offset] << (8 * i);
- }
- return num;
- }
-
-
-
-
-
-
- public static byte[] IntToBytes(int data, int length = 4)
- {
- if (length < 1 || length > 4)
- {
- length = 2;
- }
- byte[] buffer = new byte[length];
- for (int i = 0; i < length; i++)
- {
- buffer[i] = (byte)(data >> (i * 8));
- }
- return buffer;
- }
- #endregion int byte 互转
- #region Color 转为short
-
-
-
-
-
-
-
-
-
-
-
- #endregion Color 转为short
- }
- }
|