123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace WWPipeLine.Commons
- {
- /// <summary>
- /// 编码转换类
- /// </summary>
- public class EncodeHelper
- {
- private EncodeHelper()
- {
- }
- #region 判断文件流是否为UTF8字符集
- /// <summary>
- /// 判断文件流是否为UTF8字符集
- /// </summary>
- /// <param name="sbInputStream">文件流</param>
- /// <returns>判断结果</returns>
- public static bool IsUTF8(FileStream sbInputStream)
- {
- int i;
- byte cOctets; // octets to go in this UTF-8 encoded character
- 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
- /// <summary>
- /// 将GB2312转换成UTF-8
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static string GB2312ToUTF8(string content)
- {
- byte[] _byteData = GetDefaultByte(content);
- return Encoding.UTF8.GetString(_byteData);
- }
- #endregion GB2312转换成UTF-8
- #region 将字符串转换默认的Byte数组
- /// <summary>
- /// 将字符串转换默认的Byte数组
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static byte[] GetDefaultByte(string content)
- {
- return Encoding.Default.GetBytes(content);
- }
- #endregion 将字符串转换默认的Byte数组
- #region 文件转化为Base64
- /// <summary>
- /// 文件转化为Base64
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns>Base64</returns>
- 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
- /// <summary>
- /// 图象转化为Base64
- /// </summary>
- /// <param name="image">图象路径</param>
- /// <param name="format">图象格式</param>
- /// <returns>Base64</returns>
- //public string ImageToBase64String(Image image, ImageFormat format)
- //{
- // MemoryStream memory = new MemoryStream();
- // image.Save(memory, image.RawFormat);
- // string base64 = Convert.ToBase64String(memory.ToArray());
- // memory.Close();
- // memory.Dispose();
- // return base64;
- //}
- #endregion 图象转化为Base64
- #region Base64转化为图象
- /// <summary>
- /// Base64转化为图象
- /// </summary>
- /// <param name="base64">Base64</param>
- /// <returns>图象</returns>
- //public Image ImageFromBase64String(string base64)
- //{
- // MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
- // Image result = Image.FromStream(memory);
- // memory.Close();
- // memory.Dispose();
- // return result;
- //}
- #endregion Base64转化为图象
- #region 图标转化为Base64
- /// <summary>
- /// 图标转化为Base64
- /// </summary>
- /// <param name="image">图标</param>
- /// <returns>Base64</returns>
- //public string IconToBase64String(Icon image)
- //{
- // MemoryStream memory = new MemoryStream();
- // image.Save(memory);
- // string base64 = Convert.ToBase64String(memory.ToArray());
- // memory.Close();
- // memory.Dispose();
- // return base64;
- //}
- #endregion 图标转化为Base64
- #region Base64转化为图标
- /// <summary>
- /// Base64转化为图标
- /// </summary>
- /// <param name="base64">Base64</param>
- /// <returns>图标</returns>
- //public Icon IconFromBase64String(string base64)
- //{
- // MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
- // Icon result = new Icon(memory);
- // memory.Close();
- // memory.Dispose();
- // return result;
- //}
- #endregion Base64转化为图标
- #region IP 协议采用统一的校验算法
- /// <summary>
- /// IP 协议采用统一的校验算法
- /// </summary>
- /// <param name="buffer">buffer数组为整个ip包数组,需要转换成UInt16[]</param>
- /// <param name="size">size为buffer数组的长度</param>
- /// <returns>结果为0则数据正确,不为0表示通讯出错</returns>
- 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代码
- /// <summary>
- /// 生成字符串的ascii代码
- /// </summary>
- /// <param name= "s_Chinese "> 中文字符串,可夹英文 </param>
- /// <returns> </returns>
- 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;
- }
- /// <summary>
- /// 生成字符串的ascii代码
- /// </summary>
- /// <param name= "s_Chinese "> 中文字符串,可夹英文 </param>
- /// <returns> </returns>
- 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;
- }
- /// <summary>
- /// 生成ascii代码的字符串
- /// </summary>
- /// <param name= "ByteArray">ascii代码 { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 }</param>
- /// <returns> </returns>
- public static string Get_ASCII(byte[] ByteArray)
- {
- ASCIIEncoding AE = new ASCIIEncoding();
- string strAsc = AE.GetString(ByteArray);
- return strAsc;
- }
- #endregion 生成字符串的ascii代码
- #region 生成ascii代码的字符数组
- /// <summary>
- /// 生成ascii代码的字符数组
- /// </summary>
- /// <param name= "ByteArray">ascii代码 { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 }</param>
- /// <returns> </returns>
- public static char[] Gen_ASCII(byte[] ByteArray)
- {
- ASCIIEncoding AE = new ASCIIEncoding();
- char[] CharArray = AE.GetChars(ByteArray);
- return CharArray;
- }
- #endregion 生成ascii代码的字符数组
- #region 字节数组转16进制字符串
- /// <summary>
- /// 字节数组转16进制字符串
- /// </summary>
- /// <param name="bytes"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 字节数组转16进制字符串
- /// </summary>
- /// <param name="bytes"></param>
- /// <returns></returns>
- 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进制字节数组
- /// <summary>
- /// 字符串转16进制字节数组
- /// </summary>
- /// <param name="hexString"></param>
- /// <returns></returns>
- 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进制字符串异或
- /// <summary>
- /// 16进制字符串异或
- /// </summary>
- /// <param name="hexString"></param>
- /// <returns></returns>
- 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 转全角\半角的函数
- /// <summary>
- /// 转全角的函数(SBC case)
- ///
- ///任意字符串
- ///全角字符串
- ///
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// </summary>
- /// <param name="input">任意字符串</param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- ///
- /// 转半角的函数(DBC case)
- ///
- ///任意字符串
- ///半角字符串
- ///
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- 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 是否全角\半角
- /// <summary>
- /// 是否全角的函数(SBC case)
- ///
- ///任意字符串
- ///全角字符串
- ///
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// </summary>
- /// <param name="input">任意字符串</param>
- /// <returns></returns>
- public static bool IsSBC(string input)
- {
- if (2 * input.Length == Encoding.Default.GetByteCount(input))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 是否半角的函数(DBC case)
- ///
- ///任意字符串
- ///全角字符串
- ///
- ///全角空格为12288,半角空格为32
- ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// </summary>
- /// <param name="input">任意字符串</param>
- /// <returns></returns>
- public static bool IsDBC(string input)
- {
- if (input.Length == Encoding.Default.GetByteCount(input))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion 是否全角\半角
- #region 转换Image到byte[] 数组
- ///// <summary>
- ///// 转换Image到byte[] 数组
- ///// </summary>
- ///// <param name="imageIn">Image</param>
- ///// <returns>byte[] 数组</returns>
- //public static byte[] imageToByteArray(System.Drawing.Image imageIn)
- //{
- // MemoryStream ms = new MemoryStream();
- // imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
- // byte[] returnBuffer = new byte[ms.Length];
- // ms.Read(returnBuffer, 0, int.Parse(ms.Length.ToString()));
- // //清空和释放内存流
- // ms.Close();
- // ms.Dispose();
- // return returnBuffer;
- //}
- #endregion 转换Image到byte[] 数组
- #region 转换byte[]到Image
- ///// <summary>
- ///// 转换byte[]到Image
- ///// </summary>
- ///// <param name="imageIn">byte[] 数组</param>
- ///// <returns>Image</returns>
- //public static Image byteArrayToImage(byte[] byteArrayIn)
- //{
- // MemoryStream ms = new MemoryStream(byteArrayIn);
- // Image returnImage = Image.FromStream(ms);
- // ms.Close();
- // ms.Dispose();
- // return returnImage;
- //}
- #endregion 转换byte[]到Image
- #region 将字节数组转换成字符串
- /// <summary>
- /// 将字节数组转换成字符串
- /// </summary>
- /// <param name = "b">字节数组</param>
- /// <returns>返回转换后的字符串</returns>
- 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 互转
- /// <summary>
- /// byte 转int
- /// </summary>
- /// <param name="bytesData"></param>
- /// <returns></returns>
- public static int BytesToInt(byte[] bytesData)
- {
- int num = 0;
- for (int i = 0; i < bytesData.Length; i++)
- {
- num += bytesData[i] << (8 * i);
- }
- return num;
- }
- /// <summary>
- /// bytes转int
- /// </summary>
- /// <param name="bytesData">自己数据</param>
- /// <param name="offset">偏移量</param>
- /// <param name="length">字节长度最大为4最小为1,如果length 大于4或小于1 内部赋值为2</param>
- /// <returns>int值</returns>
- 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;
- }
- /// <summary>
- /// int转byte
- /// </summary>
- /// <param name="data"></param>
- /// <param name="length">字节长度最大为4最小为1,如果length 大于4或小于1 内部赋值为2</param>
- /// <returns></returns>
- 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
- /// <summary>
- /// 将Color转为Short类型
- /// </summary>
- /// <param name="color"></param>
- /// <returns></returns>
- //public static short ColorToShort(Color color)
- //{
- // return (short)(((color.R & 0xf8) << 8)
- // | ((color.G & 0xfc) << 3)
- // | ((color.B & 0xf8) >> 3));//红高5,绿高6,蓝高5
- //}
- #endregion Color 转为short
- }
- }
|