123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BaseLibRWFile
- {
- public class DataFormatConversion
- {
- #region ByteToString
- /// <summary>
- /// 字节数组到十六进制字符串
- /// </summary>
- /// <param name="bytes">待转换字节数组</param>
- /// <returns>十六进制字符串</returns>
- public static string ByteToHexStr(byte[] bytes)//byte数组转换为十六进制字符串
- {
- string returnStr = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- returnStr += bytes[i].ToString("X2");//ToString("X2") 为C#中的字符串格式控制符
- }
- }
- return returnStr;
- }
- /// <summary>
- /// Bytes to string.
- /// </summary>
- /// <param name="data">The data.</param>
- /// <returns>System.String.</returns>
- public static string ByteToString(byte[] data)
- {
- return Encoding.Default.GetString(data);
- }
- //字节变量返回8为二进制字符串
- /// <summary>
- /// 字节变量返回8位二进制字符串
- /// </summary>
- /// <param name="bValue">字节变量值</param>
- /// <returns></returns>
- public string ByteToBinary(byte bValue)
- {
- string strTemp;
- int i, strLen;
- strTemp = Convert.ToString(bValue, 2);
- if (strTemp.Length < 8)
- {
- strLen = strTemp.Length;
- for (i = 0; i < 8 - strLen; i++)
- {
- strTemp = "0" + strTemp;
- }
- }
- return strTemp;
- }
- #endregion
- /// <summary>
- /// 字符串转换为16进制
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string StringToHexString(string str)
- {
- StringBuilder s = new StringBuilder();
- foreach (short c in str.ToCharArray())
- {
- s.Append(c.ToString("X4"));
- }
- return s.ToString();
- }
- /// <summary>
- /// 字符串转16进制字节数组
- /// </summary>
- /// <param name="hexString">待转换字符串</param>
- /// <returns>字节数组</returns>
- public static byte[] HexStrTobyte(string hexString)//十六进制字符串转换为byte数组
- {
- 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).Trim(), 16);
- return returnBytes;
- }
- /// <summary>
- /// Strings to byte.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns>System.Byte[].</returns>
- public static byte[] StringToByte(string value)
- {
- return Encoding.Default.GetBytes(value);
- }
- /// <summary>
- /// 字符串转换16进制字节数组
- /// </summary>
- /// <param name="hexString"></param>
- /// <returns></returns>
- public static byte[] StrToHexByte(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).Replace(" ", ""), 16);
- return returnBytes;
- }
- public static string StrToHexBytes(string hexString)
- {
- hexString = hexString.Trim();
- byte[] b = Encoding.ASCII.GetBytes(hexString);//按照指定编码将string编程字节数组
- string[] strA = new string[b.Length];
- string strB = string.Empty;
- for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
- {
- strA[i] = Convert.ToString(b[i], 16);
- }
- int il = (int)Math.Ceiling((double)b.Length / 2);
- for (int i = 0; i < il; i++)
- {
- if (i * 2 + 1 > strA.Length - 1)
- {
- strB += "00";
- }
- else
- {
- strB += strA[i * 2 + 1];
- }
- strB += strA[i * 2];
- }
- return strB;
- }
- public static string HexStringToBinary(string[] str)
- {
- byte[] buff = new byte[str.Length * 2];
- for (int i = 0; i < str.Length; i++)
- {
- buff[i * 2] = Convert.ToByte(str[i].Substring(2, 2), 16);
- buff[i * 2 + 1] = Convert.ToByte(str[i].Substring(0, 2), 16);
- }
- string result = Encoding.Default.GetString(buff);
- result = result.Trim();
- result = result.TrimEnd('\0');
- result = result.TrimStart('\0');
- return result;
- }
- }
- }
|