| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Text;
- namespace Model
- {
- public static class ByteHelper
- {
- public static string ToHex(this byte b)
- {
- return b.ToString("X2");
- }
- public static string ToHex(this byte[] bytes)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (byte b in bytes)
- {
- stringBuilder.Append(b.ToString("X2"));
- }
- return stringBuilder.ToString();
- }
- public static string ToHex(this byte[] bytes, string format)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (byte b in bytes)
- {
- stringBuilder.Append(b.ToString(format));
- }
- return stringBuilder.ToString();
- }
- public static string ToHex(this byte[] bytes, int offset, int count)
- {
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = offset; i < offset + count; ++i)
- {
- stringBuilder.Append(bytes[i].ToString("X2"));
- }
- return stringBuilder.ToString();
- }
- public static string ToStr(this byte[] bytes)
- {
- return Encoding.Default.GetString(bytes);
- }
- public static string ToStr(this byte[] bytes, int offset, int count)
- {
- return Encoding.Default.GetString(bytes, offset, count);
- }
- public static string Utf8ToStr(this byte[] bytes, int offset, int count)
- {
- return Encoding.UTF8.GetString(bytes, offset, count);
- }
- public static byte[] Reverse(this byte[] bytes)
- {
- Array.Reverse(bytes);
- return bytes;
- }
- }
- }
|