Application 1: convert to a hexadecimal string
1 public static string ToHex (this byte B)
2 {
3 return B. ToString ("X2 ");
4}
5
6 public static string ToHex (this IEnumerable <byte> bytes)
7 {
8 var sb = new StringBuilder ();
9 foreach (byte B in bytes)
10 sb. Append (B. ToString ("X2 "));
11 return sb. ToString ();
12}
The hex string returned by the second extension is connected. In some cases, a space is used to separate the string for reading convenience. The processing is simple and examples are not given.
Application 2: convert to a Base64 string
1 public static string ToBase64String (byte [] bytes)
2 {
3 return Convert. ToBase64String (bytes );
4}
Application 3: Convert to basic data type
1 public static int ToInt (this byte [] value, int startIndex)
2 {
3 return BitConverter. ToInt32 (value, startIndex );
4}
5 public static long ToInt64 (this byte [] value, int startIndex)
6 {
7 return BitConverter. ToInt64 (value, startIndex );
8}
BitConverter class also has many methods (ToSingle, ToDouble, ToChar...), which can be expanded as above.
Application 4: convert to a specified encoded string
1 public