Convert bytearray to string in specific case
<p>I wrote a little app in Python 2.7 that compares an MSSQL and an Sqlite database and I have some problem with the binary datatypes (<code>binary</code>, <code>varbinary</code>, <code>image</code>, etc).
On the server side is an application which was written in C# that sends the data to a mobile device, but first converts the binary types to hex.<br>
For example:</p>

<p>In the database there's a column with datatype <code>binary(50)</code> and stores information like this:</p>

<pre><code>0x81B5ED7992000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</code></pre>

<p>The C# app converts it to hex with this code:</p>

<pre class="lang-cs prettyprint-override"><code>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());
</code></pre>

<p>The variables contain these data:</p>

<pre class="lang-cs prettyprint-override"><code>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
</code></pre>

<p>In my python app I am using the <a href="http://code.google.com/p/pyodbc/" rel="nofollow" title="pyodbc on Google Code">pyodbc</a> library.
From the MSSQL database, I get the data as a bytearray:</p>

<pre><code>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')
</code></pre>

<p>From the Sqlite database I can read as a unicode string:</p>

<pre><code>u'53797374656d2e427974655b5d'
</code></pre>

<p>So I need to convert that bytearray to the exact same format as the unicode string to compare them.
I've tried to find a solution in Stackoverflow, but I always get a completely different string than I expected.</p>

<p>Does anybody know how can I do this?</p>
