zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Core/Helper/StringHelper.cs

53 lines
1.5 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace ET
{
public static class StringHelper
{
public static IEnumerable<byte> ToBytes(this string str)
{
byte[] byteArray = Encoding.Default.GetBytes(str);
return byteArray;
}
public static byte[] ToByteArray(this string str)
{
byte[] byteArray = Encoding.Default.GetBytes(str);
return byteArray;
}
public static byte[] ToUtf8(this string str)
{
byte[] byteArray = Encoding.UTF8.GetBytes(str);
return byteArray;
}
public static byte[] HexToBytes(this string hexString)
{
if (hexString.Length % 2 != 0)
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
}
2021-04-11 19:50:39 +08:00
byte[] hexAsBytes = new byte[hexString.Length / 2];
2021-04-08 20:09:59 +08:00
for (int index = 0; index < hexAsBytes.Length; index++)
{
string byteValue = "";
byteValue += hexString[index * 2];
byteValue += hexString[index * 2 + 1];
hexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return hexAsBytes;
}
public static string Fmt(this string text, params object[] args)
{
return string.Format(text, args);
}
}
}