473,511 Members | 14,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BitConverter.ToString() bug

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?
Nov 16 '05 #1
2 2119
Opa1 <op***********@yahoo.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2


Thanks a lot Jon, I will try your fix.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16211
by: Greg Ennis | last post by:
If I run the following code: Byte bytes = System.Text.Encoding.ASCII.GetBytes("test"); return BitConverter.ToString(bytes); I get the string "74-65-73-74" back. My question is, what is the...
1
3384
by: Gabe Jahn | last post by:
Basically I've used the BitConverter to convert a byte into a string. Now how do I convert it back to a byte? Hopefully without creating my own algorithm to accomplish this? Thanks...
5
5355
by: george r smith | last post by:
Hi, I created a byte array --byte aByteArray = new byte; and set aByteArray and aByteArray to '1'. Because I want to create a ulong (UInt64) with these values I do: UInt64 bb; bb =...
3
6383
by: Timothy V | last post by:
Hi, I have a byte that i want to convert into a string. I use the BitConverter.ToString() method. Now, how do I convert that string back into a byte? Thanks in advance, Tim.
1
2211
by: Michael Davidov | last post by:
byte array = {0x00,0x04}; short ashort = BitConverter.ToInt16(array,0); Console.WriteLine(ashort.ToString()); Why am I wrong to think that this should return a 4 instead of 1024? Also, is...
7
8092
by: Lenn | last post by:
This probably something stupid, or I am missing some fundemantal concept, but I can't figure this one out. Consider the following code: byte bd = new byte; bd = 0x00; bd = 0x01; ...
5
4569
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. I have a problem with getting short value from 2 byte array. I have this code. There are 2 short values in bytes. byte cast = { 18, 152, 00, 80 }; Int32 port = BitConverter.ToInt16(cast,...
1
1983
by: DR | last post by:
mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true in tsql: select...
5
5937
by: DaveD | last post by:
Can anyone help me get this compiled ? void Write<T>(T val) { byte bytes = BitConverter.GetBytes(val); Array.Reverse(bytes); writer.Write(bytes); } The problem is that for T=bool,...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.