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);
}
}
}
}
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
var txt = File.ReadAllText("文件名");
var b = Regex.Matches(txt, @"\w{2}").Cast<Match>().Select(x => (byte)Convert.ToInt32(x.Value, 16)).ToArray();
File.WriteAllBytes("文件名", b);
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);
}
}
}
}