C# 在特定情况下,将bytearray转换为字符串

C# 在特定情况下,将bytearray转换为字符串,c#,python,C#,Python,我用Python2.7编写了一个比较MSSQL和Sqlite数据库的小应用程序,我对二进制数据类型有一些问题(binary,varbinary,image,等等)。 服务器端是一个用C语言编写的应用程序,它将数据发送到移动设备,但首先将二进制类型转换为十六进制。 例如: 数据库中有一列数据类型为binary(50),存储的信息如下: 0x81B5ED79920000000000000000000000000000000000000000000000000000000000000000000000

我用Python2.7编写了一个比较MSSQL和Sqlite数据库的小应用程序,我对二进制数据类型有一些问题(
binary
varbinary
image
,等等)。 服务器端是一个用C语言编写的应用程序,它将数据发送到移动设备,但首先将二进制类型转换为十六进制。
例如:

数据库中有一列数据类型为binary(50),存储的信息如下:

0x81B5ED7992000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
C#应用程序使用以下代码将其转换为十六进制:

var sb=new StringBuilder();
某人加上(“”);
byte[]data=Encoding.UTF8.GetBytes(value.ToString());
foreach(数据中的字节b)
{
sb.Append(string.Format(“{0:x2}”,b));
}
某人加上(“”);
valuesStringBuilder.Append(sb.ToString());
变量包含以下数据:

值
{字节[50]}
[0]: 129
[1]: 181
[2]: 237
[3]: 121
[4]: 146
[5]: 0
[6]: 0
[7]: 0
[8]: 0
[9]: 0
[10]: 0
[11]: 0
[12]: 0
[13]: 0
[14]: 0
[15]: 0
[16]: 0
[17]: 0
[18]: 0
[19]: 0
[20]: 0
[21]: 0
[22]: 0
[23]: 0
[24]: 0
[25]: 0
[26]: 0
[27]: 0
[28]: 0
[29]: 0
[30]: 0
[31]: 0
[32]: 0
[33]: 0
[34]: 0
[35]: 0
[36]: 0
[37]: 0
[38]: 0
[39]: 0
[40]: 0
[41]: 0
[42]: 0
[43]: 0
[44]: 0
[45]: 0
[46]: 0
[47]: 0
[48]: 0
[49]: 0
value.ToString()
“系统字节[]”
数据
{字节[13]}
[0]: 83
[1]: 121
[2]: 115
[3]: 116
[4]: 101
[5]: 109
[6]: 46
[7]: 66
[8]: 121
[9]: 116
[10]: 101
[11]: 91
[12]: 93
某人
{'53797374656d2e427974655b5d'}
容量:32
长度:28
最大容量:2147483647
在我的python应用程序中,我正在使用库。 从MSSQL数据库中,我以字节数组的形式获取数据:

bytearray(b'\x81\xb5\xedy\x92\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
从Sqlite数据库中,我可以读取unicode字符串:

u'53797374656d2e427974655b5d'
所以我需要将bytearray转换为与unicode字符串完全相同的格式来比较它们。 我试图在Stackoverflow中找到一个解决方案,但我总是得到一个完全不同于预期的字符串

有人知道我该怎么做吗?

下面是一个例子:

using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1.Text = "I am a string";
}

private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Convert to Hex")
{
this.LocalString = this.richTextBox1.Text;

ConvertToByteArray(this.LocalString);

this.button1.Text = "Convert to String";
}
else
{
string HexString = this.richTextBox1.Text.Trim();
HexString = HexString.Replace("'", "");

ConvertToString(HexString);

this.button1.Text = "Convert to Hex";
}
}

private byte[] LocalByteArray = new byte[50];

private string LocalString = string.Empty;

private void ConvertToByteArray(string myString)
{
// Convert the String passed to a Byte Array for Hex Conversion...
this.LocalByteArray = Encoding.UTF8.GetBytes(myString);

// Create a new StringBuilder Reference...
StringBuilder stringBuilder = new StringBuilder();

// Append Start Char...
stringBuilder.Append("'");

// Loop through and append each converted Byte (Converted to Hex) to the String that we are building...
foreach (byte b in this.LocalByteArray)
{
stringBuilder.Append(b.ToString("X2"));
}

// Append End Char...
stringBuilder.Append("'");

// Assign the String to TextBox...
this.richTextBox1.Text = stringBuilder.ToString();

}

private void ConvertToString(string HexString)
{
// Create a new StringBuilder Reference...
StringBuilder stringBuilder = new StringBuilder();

// Loop through and append each converted Byte (Converted from Hex) to the String that we are building...
for (int i = 0; i <= HexString.Length - 2; i += 2)
{
stringBuilder.Append(Convert.ToString(Convert.ToChar(Int32.Parse(HexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
}

// Assign the String to TextBox...
this.richTextBox1.Text = stringBuilder.ToString();
}
}
}
使用系统;
使用系统数据;
使用System.Linq;
使用系统文本;
使用系统图;
使用System.Windows.Forms;
使用系统组件模型;
使用System.Collections.Generic;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.richTextBox1.Text=“我是一个字符串”;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(button1.Text==“转换为十六进制”)
{
this.LocalString=this.richTextBox1.Text;
ConvertToByteArray(this.LocalString);
this.button1.Text=“转换为字符串”;
}
其他的
{
string HexString=this.richTextBox1.Text.Trim();
HexString=HexString.Replace(“,”);
转换字符串(十六进制字符串);
this.button1.Text=“转换为十六进制”;
}
}
专用字节[]LocalByteArray=新字节[50];
私有字符串LocalString=string.Empty;
私有void ConvertToByteArray(字符串myString)
{
//将传递给字节数组的字符串转换为十六进制转换。。。
this.LocalByteArray=Encoding.UTF8.GetBytes(myString);
//创建新的StringBuilder引用。。。
StringBuilder StringBuilder=新的StringBuilder();
//附加开始字符。。。
stringBuilder.Append(“”);
//循环并将每个转换的字节(转换为十六进制)附加到我们正在构建的字符串中。。。
foreach(此.LocalByteArray中的字节b)
{
stringBuilder.Append(b.ToString(“X2”));
}
//附加结束字符。。。
stringBuilder.Append(“”);
//将字符串指定给文本框。。。
this.richTextBox1.Text=stringBuilder.ToString();
}
私有void ConvertToString(字符串hextString)
{
//创建新的StringBuilder引用。。。
StringBuilder StringBuilder=新的StringBuilder();
//循环并将每个转换的字节(从十六进制转换)附加到我们正在构建的字符串中。。。

对于(int i=0;i),这两个值没有我能看到的相关性。
5379..
值是十六进制编码的UTF8,(
'53797374656d2e427974655b5d'。decode('hex')。decode('UTF8')
生成
u'System.Byte[]
)。另一个值仅包含5字节的信息,几乎不足以对该字符串进行编码。c部分似乎已损坏,请看c代码不是我的,我只是问了应用程序的开发人员,所以不知道它是否已损坏,但他使用该值并工作。也许我会检查c应用程序中原始数据的外观,希望这有所帮助。
value.ToString()
为数据库中的任何二进制字段值返回“System.Byte[]”。这显然是错误的(数据丢失)。请阅读。@Sebastian您是对的,C代码确实是坏的。在开发人员修复后,我将尝试在python中实现相同的方法,如果我不能实现,我将返回并更改帖子。:)