sharp-extensions/StringExtensions.cs

31 lines
765 B
C#

using System;
namespace sharp.extensions
{
public static class StringExtensions
{
public static string[] splitConstantWidth(this string s,int len){
string[] result = new string[(s.Length+len-1) / len];
for (int n = 0; n < result.Length;n++){
result[n] = s.Substring(n * len, (n < result.Length - 1) ? len : s.Length % len);
}
return result;
}
public static byte[] toBytes(this string hexstring)
{
byte[] bytes = new byte[hexstring.Length >> 1];
for (int n = 0; n < hexstring.Length >> 1; n++)
{
bytes[n] = Convert.ToByte(hexstring.Substring(n << 1, 2), 16);
}
return bytes;
}
public static String toHexString(this byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", String.Empty);
}
}
}