[英]Convert bytearray to string in specific case
我在Python 2.7中编写了一个小应用程序来比较MSSQL和Sqlite数据库,我对二进制数据类型( binary
, varbinary
, image
等)有一些问题。 在服务器端是一个用C#编写的应用程序,它将数据发送到移动设备,但首先将二进制类型转换为十六进制。
例如:
在数据库中有一个数据类型为binary(50)
的列,并存储如下信息:
0x81B5ED7992000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
C#app使用以下代码将其转换为十六进制:
var sb = new StringBuilder();
sb.Append("'");
byte[] data = Encoding.UTF8.GetBytes(value.ToString());
foreach (byte b in data)
{
sb.Append(string.Format("{0:x2}", b));
}
sb.Append("'");
valuesStringBuilder.Append(sb.ToString());
变量包含以下数据:
value
{byte[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()
"System.Byte[]"
data
{byte[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
sb
{'53797374656d2e427974655b5d'}
Capacity: 32
Length: 28
MaxCapacity: 2147483647
在我的python应用程序中,我使用的是pyodbc库。 从MSSQL数据库中,我将数据作为bytearray获取:
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();
}
}
}
也许这会对你有所帮助。 它有点被黑了,可以整理一下。 但是给你一个主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.