1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System.Text;
- using System;
- namespace Ropin.Environmentally.LoRaService
- {
- public class StringHelper
- {
-
-
-
-
-
-
- public static string StringToHexString(string s, Encoding encode)
- {
- byte[] b = encode.GetBytes(s);
- string result = string.Empty;
- for (int i = 0; i < b.Length; i++)
- {
- result += " " + Convert.ToString(b[i], 16);
- }
- return result.Substring(1, result.Length - 1);
- }
-
-
-
-
-
-
- public static string HexStringToString(string hs, Encoding encode)
- {
-
- string[] chars = hs.Split(' ');
- byte[] b = new byte[chars.Length];
-
- for (int i = 0; i < chars.Length; i++)
- {
- b[i] = Convert.ToByte(chars[i], 16);
- }
-
- return encode.GetString(b);
- }
-
-
-
-
-
- public static byte[] StringToHexByte(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;
- }
-
-
-
-
-
- public static string ByteToHexString(byte[] bytes)
- {
- string returnStr = "";
- if (bytes != null)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- returnStr += " " + bytes[i].ToString("X2");
- }
- }
- return returnStr.Substring(1, returnStr.Length - 1);
- }
- }
- }
|