Hallo i am Working with Stringbuilder Array,i want to convert byte in from a Byte array and save it in a Stringbuilder Array,that is my code,but is not runing like i want,i have that failure :object reference was not specified on an object instance.

private string Byte_To_Hex_Ar(Byte[] Buff) 
        {
            StringBuilder[] My_hex = new StringBuilder[Buff.Length];
            for (int i = 0; i <= My_hex.Length; i++ )
            {

                  My_hex[i].Append(Buff[i].ToString()); //How  can i Move away from this Situation???
                MessageBox.Show(My_hex[i].ToString());



            }
            return My_hex.ToString();
        
        
        
        
        }

thx in advance

Member Avatar for stbuchok

I think you need to change this i <= My_hex.Length to this i < My_hex.Length. Is it only ever happening on the last index?

i did it but it never change

You dont need to use StringBuilder class as array. You can simple do:
REMEBER: before putting bytes into stirng, you will have to convert it, otherwise you will not get the correct result!!

private string Byte_To_Hex_Ar(Byte[] data) 
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in data)
            { 
                sb.Append(b.ToString("X")); 
            }
            return sb.ToString();
        }

---------------------------
TO ADD:
If you will pass these data:

byte[] values = { 0x1, 0x2, 0x10, 0xFF };

string val = ByteArrayToString(values);

The returned string will be "1210FF"

This is how you do it.

but do you think that this method is optimal with a byte Array bigger als 935 Byte???because to have this Byte array,i have read all the byte of Binary logfile,and now i want to convert every byte in his Original Value(integer,float,or Char) i can not save all that in a String,it because of that why i am using Stringbuilder.I hope you have you have understood what i mean.Thx in advance

Not sure what you mean that you can't use a string... all StringBuilder does is create a string. From some quick googling the "maximum" size of a string (through a stringbuilder) ~500million chars, or ~250million bytes in hex.

StringBuilders start with a default capacity (or you can set the initial capacity), and when it reaches that capacity, it automatically expands (by doubling the current cap.). So the StringBuilder will continue to expand in size as you keep writing to it. The "maximum" size limit on the string comes into play when the StringBuilder has to double in size numerous times. This would really only happen if your builder starts small (with the default capacity, for example) and ends up needing to be very large. You'll run out of contiguous memory, and the builder can't double its size. If you initialize the builder with a large amount of space you shouldn't run into this problem.

But if you're working in the order of 1000's of bytes, and not millions of bytes, you should't ever run into a size problem.

The discussion I read on this is outdated, but I can't imagine that as time passes these limits would decrease... (http://www.pcreview.co.uk/forums/maximum-string-length-c-and-net-t2539908.html)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.