BitConverter.ToString() bug

  • Thread starter Thread starter Opa1
  • Start date Start date
O

Opa1

using BitConverter.ToString() in Compact Framework has some bugs.

The resulting hex string does not have a consistent format for each
byte converted from the input byte array.

for example:
Byte array with values,
(9,0,0,0,115,105,103,110,97,116,117,114,101,224,0,0,0,51,0,0,0,1)

result in,

"9-00-00-00-73-69-67-6E-61-74-75-72-65-E0-00-00-00-33-00-00-00-1"

should be,

"09-00-00-00-73-69-67-6E-61-74-75-72-65-E0-00-00-00-33-00-00-00-01"

I read some where that it has been fixed. I have .NET 1.1 Version
1.1.4322.
Is there a new update.

Does someone have a workaround function?
 
Opa1 said:
using BitConverter.ToString() in Compact Framework has some bugs.

Yup - apparently it's fixed in SP2 though.
I read some where that it has been fixed. I have .NET 1.1 Version
1.1.4322.
Is there a new update.

There's SP2 for the Compact Framework, where it's apparently fixed.
It's easy to mock up though.
Does someone have a workaround function?

Well:

public static string ByteArrayToString (byte[] bytes)
{
if (bytes==null || bytes.Length==0)
{
return "";
}
StringBuilder builder = new StringBuilder
(bytes.Length*3-1);

bool first=true;
foreach (byte b in bytes)
{
if (!first)
{
builder.Append("-");
}
else
{
first = false;
}
builder.AppendFormat ("{0:x2}", b);
}
return builder.ToString();
}

That should do it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top