C# 字符串转码后操作二进制文件
- String转码后写入二进制文件,读二进制文件进行解码返回。
public class BinaryClass
{
/// <summary>
/// 写二进制文件
/// </summary>
/// <param name="binFile"></param>
/// <param name="str"></param>
public static void WriterBinaryFile(string binFile, params string[] paramsStrs)
{
var outputStream = File.Create(binFile);
using (var writer = new BinaryWriter(outputStream))
{
foreach (var str in paramsStrs)
{
string strResult = StringToBinary(str);
writer.Write(strResult);
}
}
}
/// <summary>
/// 读二进制文件 String
/// </summary>
/// <param name="binFile"></param>
/// <returns></returns>
public static string ReadBinaryFileString(string binFile)
{
string strRes = string.Empty;
var inputStream = File.Open(binFile, FileMode.Open);
using (var reader = new BinaryReader(inputStream))
{
strRes = reader.ReadString();
}
return strRes;
}
/// <summary>
/// 读二进制文件 Array(string[])
/// </summary>
/// <param name="binFile"></param>
/// <param name="paramsStrs"></param>
/// <returns></returns>
public static string[] ReadBinaryFileArray(string binFile, params string[] paramsStrs)
{
string[] strResArr = new string[paramsStrs.Length];
var inputStream = File.Open(binFile, FileMode.Open);
using (var reader = new BinaryReader(inputStream))
{
for (int i = 0; i < paramsStrs.Length; i++)
{
string str = reader.ReadString();
strResArr[i] = BinaryToString(str);
}
}
return strResArr;
}
/// <summary>
/// 读二进制文件 Object
/// </summary>
/// <param name="binFile"></param>
/// <returns></returns>
public static CacheBinaryDto? ReadBinaryFileObject(string binFile)
{
string strRes = string.Empty;
var inputStream = File.Open(binFile, FileMode.Open);
using (var reader = new BinaryReader(inputStream))
{
strRes = reader.ReadString();
}
return JsonConvert.DeserializeObject<CacheBinaryDto>(strRes);
}
/// <summary>
/// 读二进制文件 List<Object>
/// </summary>
/// <param name="binFile"></param>
/// <param name="paramsStrs"></param>
/// <returns></returns>
public static List<CacheBinaryDto> ReadBinaryFileListObject(string binFile, params string[] paramsStrs)
{
string[] strResArr = ReadBinaryFileArray(binFile, paramsStrs);
List<CacheBinaryDto> result = new List<CacheBinaryDto>();
foreach (var str in strResArr)
{
result.Add(JsonConvert.DeserializeObject<CacheBinaryDto>(str));
}
return result;
}
/// <summary>
/// 将字符串转成二进制
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StringToBinary(string str)
{
byte[] data = Encoding.ASCII.GetBytes(str);
StringBuilder result = new StringBuilder(data.Length * 8);
foreach (byte b in data)
{
result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
result.Append("001&01001*");
}
return result.ToString();
}
/// <summary>
/// 将二进制转成字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string BinaryToString(string str)
{
System.Text.RegularExpressions.CaptureCollection cs = System.Text.RegularExpressions.Regex.Match(str.Replace("001&01001*", ""), @"([01]{8})+").Groups[1].Captures;
byte[] data = new byte[cs.Count];
for (int i = 0; i < cs.Count; i++)
{
data[i] = Convert.ToByte(cs[i].Value, 2);
}
string strRes = Encoding.ASCII.GetString(data, 0, data.Length);
return strRes;
}
}
public class CacheBinaryDto
{
public string Key { get; set; }
public string Content { get; set; }
}