Tag: 字节

c#将byte转换为string并写入txt文件

我如何转换例如byte[] b = new byte[1]; b[1]=255 byte[] b = new byte[1]; b[1]=255到字符串? 我需要一个字符串变量,其值为“255” string text= “255”; 然后将其存储在文本文件中?

C#将字符转换为字节(hex表示)

这似乎是一个容易的问题,但我无法弄清楚。 我需要转换此字符<字节(hex表示),但如果我使用 byte b = Convert.ToByte(‘<'); 我得到60 (十进制表示)而不是3c 。

替换二进制文件中的字节序列

将二进制文件中的字节序列替换为相同长度的其他字节的最佳方法是什么? 二进制文件非常大,大约50 MB,不应该一次加载到内存中。 更新:我不知道需要替换的字节位置,我需要先找到它们。

如何在块中使用File.ReadAllBytes

我正在使用此代码 string location1 = textBox2.Text; byte[] bytes = File.ReadAllBytes(location1); string text = (Convert.ToBase64String(bytes)); richTextBox1.Text = text; 但是当我使用一个太大的文件时,我会遇到内存exception。 我想在块中使用File.ReadAllBytes 。 我见过这样的代码 System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open); byte[] buf = new byte[BUF_SIZE]; int bytesRead; // Read the file one kilobyte at a time. do { bytesRead = fs.Read(buf, 0, BUF_SIZE); // ‘buf’ contains the last 1024 […]

从字节数组运行程序而不创建临时文件。 C#

我有许多存储在IIS服务器(MSSQL)上的.exe文件,其中包含报告和对服务器上文件的访问权限。 (这些文件将在星期日更改。) 连接到SQL Server并选择.exe文件后,我正在下载(在SQL中选择),现在我有一个分配给变量的字节数组。 我无法在未知目录中创建像“temp.exe”这样的临时文件,因为我知道有很多方法可以理解新创建的文件目录和… 这是不安全的,因为我的用户是专业的,如果他们中的一个知道这些方式…… 所以,我想知道是否可以从字节数组运行.exe文件(默认为从“Windows资源管理器”运行)而不创建临时文件? tnx update:exe文件是.net和Manager将上传新文件或更改文件。

将byte 转换为图像

我已将一个Image作为byte[]上传到我的数据库,现在我试图将其显示出来。 出现错误 – 用户代码未处理参数exceptionParameter is not valid 在这条线上 newImage = System.Drawing.Image.FromStream(stream); 这是我的代码 for (int i = 0; i < topRatingList.Count; i++) { int commentRating = topRatingList[i].CommentRating; int drinkID = topRatingList[i].DrinkID; if (i == 0) { DrinkMenuDAO drink = DrinkMenuBLL.getDrinkMenu(drinkID); LinkButton1.Text = drink.DrinkName; ImageButton1.ImageUrl = byteArrayToImage(drink.DrinkImage); } } private string byteArrayToImage(byte[] byteArrayIn) { System.Drawing.Image newImage; string […]

C#位图图像,字节数组和流!

我有一个函数将文件提取到字节数组(数据)。 int contentLength = postedFile.ContentLength; byte[] data = new byte[contentLength]; postedFile.InputStream.Read(data, 0, contentLength); 后来我使用这个字节数组来构造一个System.Drawing.Image对象(其中data是字节数组) MemoryStream ms = new MemoryStream(data); Image bitmap = Image.FromStream(ms); 我得到以下exception“ArgumentException:参数无效。” 原始发布的文件包含500k jpeg图像… 任何想法为什么这不起作用? 注意:我向你保证我有一个有效的理由转换为字节数组然后转换为内存流!!

将字节数组转换为Int24

我正在使用BinaryReader读取文件。 我想要在地址0x37E处提取数据,但它是int24。 所以即使我读了3个字节,我也无法将其转换为int24。 你有什么建议吗? 我正在使用C#并正在研究STFS包的东西。

如何使用哈希字节比较2个图像是否相同?

private void button1_Click(object sender, EventArgs e) { Bitmap im1 = new Bitmap(@”C:\Users\user\Downloads\CaptchaCollection\1.png”); Bitmap im2 = new Bitmap(@”C:\Users\user\Downloads\CaptchaCollection\2.png”); if (HashImage(im1) == HashImage(im2)) { MessageBox.Show(“Same Image”); } else { MessageBox.Show(“Different Image”); } } 如果单击该按钮,它将比较这两个图像。 以下是用于散列图像的代码。 public byte[] HashImage(Bitmap image) { var sha256 = SHA256.Create(); var rect = new Rectangle(0, 0, image.Width, image.Height); var data = image.LockBits(rect, ImageLockMode.ReadOnly, […]

相当于Python的“struct.pack / unpack”的C#?

我是一位经验丰富的Python开发人员,并且已经开始喜欢它的许多便利。 我实际上已经知道C#已有一段时间了,但最近已经进入了一些更高级的编码。 我想知道的是,是否有办法将C#中的字节数组“解析”为一组(大小不同的)项目。 想象一下,我们有这个: python: import struct byteArray = “\xFF\xFF\x00\x00\x00\xFF\x01\x00\x00\x00” numbers = struct.unpack(“<LHL",byteArray) print numbers[0] # 65535 print numbers[1] # 255 print numbers[2] # 1 newNumbers = [0, 255, 1023] byteArray = struct.pack("<HHL",newNumbers) print byteArray # '\x00\x00\xFF\x00\xFF\x03\x00\x00' 我希望在C#中实现相同的效果,而不是像这样使用庞大,混乱的代码: C#: byte[] byteArray = new byte[] { 255, 255, 0, 0, 0, 255, 1, 0, 0, 0 […]