字符串插入字符 ?
/// <summary>
/// 每隔n个字符插入一个字符
/// </summary>
/// <param name="input">源字符串</param>
/// <param name="interval">间隔字符数</param>
/// <param name="value">待插入值</param>
/// <returns>返回新生成字符串</returns>
public static string InsertFormat(string input, int interval, string value)
{
for (int i = interval; i < input.Length; i += interval + 1)
input = input.Insert(i, value);
return input;
}
10进制转16进制 ?
static public string Get16string(string num)
{
int i = int.Parse(num);
if (i > 9)
{
num = Convert.ToString(i, 16);
if (num.Length % 2 != 0)
{
num = "0" + num;
}
}
else
{
num = "0" + num;
}
return num;
}
上面这个方法,只会补一个0, 假如你输入的是一个1,返回的就是01,但是你想要的是:0001,可以在输出之后使用这个方法
static public string Get16_4wei_String(string num)
{
Int64 i = Int64.Parse(num);
return i.ToString("X4").ToUpper();
}
这样你就可以得到 4位的 16进制,:0001,会自动进行补0?
16进制转10进制
static public string Get10string(string num)
{
int RtnMsg10 = int.Parse(num.Replace(" ", ""), System.Globalization.NumberStyles.HexNumber);
return RtnMsg10.ToString();
}
16进制转2进制 ?
//输出二进制,每4个就会有一个空格
public static string HexString2BinString(string hexString)
{
try
{
string result = string.Empty;
foreach (char c in hexString)
{
int v = Convert.ToInt32(c.ToString(), 16);
int v2 = int.Parse(Convert.ToString(v, 2));
// 去掉格式串中的空格,即可去掉每个4位二进制数之间的空格,
result += string.Format("{0:d4} ", v2);
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
倒叙输出二进制
static public string DaoXuData(string data)
{
// 0001 1110
// 0000 0000
int iData = data.Length - 1;
string NewData = "";
for (int i = iData; i > -1; i--)
{
NewData += data[i].ToString();
}
return NewData;
}
16进制转ASCII
public static string HexStringToASCII(string hexstring)
{
byte[] bt = strToToHexByte(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
16进制转Byte[] ?
#region 字符串转16进制,返回Byte[]
static public byte[] strToToHexByte(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).Trim(), 16); return returnBytes;
}
#endregion
暂时先这样,以后有的话,继续进行更新
|