Kestami
Kestami

Reputation: 2073

Is a byte[]'s byte.length() the maximum size or actual size?

I feel like I'm taking 2 steps back with this question, but something is confusing me a little. I'm working with some communication via TCP/IP and byte[]'s.

So I start building my byte[] array, and the third byte requires the length of the byte[]. If I declare my byte like so;

byte[] bytesToSend = new byte[119];

and then fill in the first three bytes with data..

bytesToSend[0] = 0x40;
bytesToSend[1] = 0x40;
bytesToSend[2] = Encoding.ASCII.GetBytes(bytesToSend.Length.ToString())[0];

and finally just print out the third byte, which should contain the length;

MessageBox.Show(BitConverter.ToString(bytesToSend));

should I be expecting it to return the byte size of 119, or is this just the maximum size? Currently it's returning hex "0x31", which as far as i'm aware doesn't equal 119 or 3. This is no doubt something simple / fundamental I'm missing, but could someone point me in the right direction?

Upvotes: 0

Views: 1405

Answers (4)

JLRishe
JLRishe

Reputation: 101748

This:

Encoding.ASCII.GetBytes(bytesToSend.Length.ToString())[0];

first converts the value "119" (the string value of the array's length) to the byte array {0x31, 0x31, 0x39}, the ascii values for 1, 1, 9. Then you are taking the first of those and assigning it to bytesToSend[2]. And that is how you are obtaining 0x31.

So to answer your question, the length of the array is the length that you gave it, i.e. 119 in this case.

Upvotes: 0

Rawling
Rawling

Reputation: 50174

bytesToSend.Length will be 119. However, rather than storing 119 in bytesToSend[2], you're

  • Converting it to a string, "119"
  • Converting that string to its ASCII representation in a byte array, [49, 49, 57]
  • Taking the first byte of that result, 49 or 0x31.

You just need to do

bytesToSend[2] = (byte)bytesToSend.Length;

although this will break if you're sending more than 255 bytes,

Upvotes: 5

Rotem
Rotem

Reputation: 21947

Seems you are unclear on what you are actually doing here:

bytesToSend[2] = Encoding.ASCII.GetBytes(bytesToSend.Length.ToString())[0];

Let's break this line down:

bytesToSend.Length 

Returns the integer 119

.ToString()

Returns the string "119"

Encoding.ASCII.GetBytes("119")

Returns a byte array of the characters that make up the ascii string "119".

[0]

Get just the first byte of this array, which would equal 0x31, because this is the ascii code for the character '1'.

Also, there is no such thing as an actual size of a c# Array. There is only a Length. If you mean to ask how many of the elements contain non-zero bytes, that's another story, but once you declare

byte[] bytesToSend = new byte[119];

You get a byte array with 119 elements, and it's Length will always be 119, regardless of how many elements you have modified.

If you need to use a collection with a more dynamic size, consider using a List<byte> instead.

Upvotes: 6

Anton Kovalenko
Anton Kovalenko

Reputation: 21517

bytesToSend.Length == 119
bytesToSend.Length.ToString() == "119"
Encoding.ASCII.GetBytes(bytesToSend.Length.ToString()) == {0x31,0x31,0x39}
Encoding.ASCII.GetBytes(bytesToSend.Length.ToString())[0] == 0x31

Upvotes: 1

Related Questions