Problem with the length of byte arrays and the length (in bytes) of diffrent datatypes 
Author Message
 Problem with the length of byte arrays and the length (in bytes) of diffrent datatypes

Hi!

At the moment I am working on a application that sends and recives
arrays of bytes. And in doing so I have encountered a few things that
make me a bit confused...

----- THE SAMPLE CODE -------

Dim ByteArray(7) As Byte
Dim intValue As Int32 = 12

Console.Write(Encoding.ASCII.GetByteCount(intValue) & vbNewLine)

ByteArray = BitConverter.GetBytes(intValue)
Console.Write(ByteArray.Length & vbNewLine)

Console.Write(ByteArray(5))

-------- THE OUPUT ----------

2
4
An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in abc.exe
Additional information: Index was outside the bounds of the array.

------ THE QUESTIONS --------

1. I have figured out that "Encoding.ASCII.GetBytes" gives me the
chars (2 bytes) while "BitConverter.GetBytes" gives me the acctual
integer (4 bytes). And I know now that
"Encoding.ASCII.GetByteCount(intValue)" gives me the number of chars
(2), but what if I want to count the bytes of "intValue" when treated
as a Int32? I want it to return a 4...

2. How come that after I have put a 4 byte value into the array the
length property gives me 4 bytes/pos and not 8 as it was declared? And
why can't I read or write to, for example byte 7!?!

3. If I want to read and write random bytes in my array, must I then
first fill the array with dummy values?

-----------------------------

Regards,

/Jonas



Sat, 14 Aug 2004 23:50:39 GMT  
 Problem with the length of byte arrays and the length (in bytes) of diffrent datatypes

Quote:
> Hi!

> At the moment I am working on a application that sends and recives
> arrays of bytes. And in doing so I have encountered a few things
> that make me a bit confused...

> ----- THE SAMPLE CODE -------

> Dim ByteArray(7) As Byte
> Dim intValue As Int32 = 12

> Console.Write(Encoding.ASCII.GetByteCount(intValue) & vbNewLine)

> ByteArray = BitConverter.GetBytes(intValue)
> Console.Write(ByteArray.Length & vbNewLine)

> Console.Write(ByteArray(5))

> -------- THE OUPUT ----------

> 2
> 4
> An unhandled exception of type 'System.IndexOutOfRangeException'
> occurred in abc.exe
> Additional information: Index was outside the bounds of the array.

> ------ THE QUESTIONS --------

> 1. I have figured out that "Encoding.ASCII.GetBytes" gives me the
> chars (2 bytes) while "BitConverter.GetBytes" gives me the acctual
> integer (4 bytes). And I know now that
> "Encoding.ASCII.GetByteCount(intValue)" gives me the number of
> chars (2), but what if I want to count the bytes of "intValue"
> when treated as a Int32? I want it to return a 4...

Use the BitConverter.
Or Len(intValue)

Quote:
> 2. How come that after I "have put a 4 byte value into the array
> the length property gives me 4 bytes/pos and not 8 as it was
> declared? And why can't I read or write to, for example byte 7!?!

GetBytes returns an array. It contains 4 bytes. It's assigned to ByteArray.

Quote:
> 3. If I want to read and write random bytes in my array, must I
> then first fill the array with dummy values?

After assigning the array to ByteArray, you can changed the values. The
content of the array before assignment is uninteresting because a new array
is assigned.

Armin



Sun, 15 Aug 2004 03:36:34 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Put Byte Array Descriptor Length = 12 bytes?

2. Using fixed-length strings/byte arrays

3. length of array of bytes

4. Need to convert a 16 byte array of bytes to 4 byte integer

5. Getting the length of selected files in a File List Box (sum in bytes)

6. length of a string in bytes

7. max length of a row (in bytes)

8. Copy bytes from an Integer to a location of bytes array

9. VB6 Byte Array to Byte()

10. converting a varible length record to a fixed length rec

11. Newbie: creating a fixed-line-length file from a variable-line-length file

12. ADOX: Extending a fixed length Jet string field length

 

 
Powered by phpBB® Forum Software