using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 将照片转换成 16进制字符串:
string strData = "";
using (FileStream fsReade = new FileStream("00.jpg", FileMode.OpenOrCreate, FileAccess.Read))
{
// 声明一个byte数组用来读取照片:
byte[] buf = new byte[60000];
int n = 0;
int len = 0;
// 循环读取照片,并存入byte数组中:
while (true)
{
len = fsReade.Read(buf, n, 1024);
if (len == 0)
{
break;
}
n = n + len;
}
for (int i = 0; i < n; i++)
{
// 将byte数组中的每一个字节都转换成16进制字符串:
strData += buf[i].ToString("X2");
}
}
// 将16进制字符串转换成照片:
using (FileStream fsWrite = new FileStream("11.jpg", FileMode.OpenOrCreate, FileAccess.Write))
{
// 声明一个字节数组,长度为16进制字符串长度的一半:
byte[] buf = new byte[strData.Length / 2];
for (int i = 0; i < buf.Length; i++)
{
// 由于16进制字符串都是两个一组,所以需要将两个字符一起转换成字节
buf[i] = Convert.ToByte(strData.Substring(i * 2, 2), 16);
}
// 将字节数组写入照片中:
fsWrite.Write(buf, 0, buf.Length);
}
}
}
}
C#学习笔记之照片与16进制字符串之间的转换
最新推荐文章于 2023-01-29 17:49:27 发布