Trouble defining a bytearray!!

  • Thread starter Thread starter Harold S
  • Start date Start date
H

Harold S

Hello,
I am having trouble defining a byte array in .Net


When i go this route it doesnt like the [] definition
Dim xxxx[] As New Bytearray = {"0x45", "0x70", "0x22", "0x48", "0x9d",
"0x17", "0x22", " 0x11", " 0x7f", " 0x96", "0x5f", "0x8f", "0x5c", " 0x96",
"0x65", "0xc2", "0x2d", "0x0c", "0x67", " 0xc7"}



When I go this route

Dim xxx() As Byte = {"0x45", "0x70", "0x22", "0x48", "0x9d", "0x17", "0x22",
" 0x11", " 0x7f", " 0x96", "0x5f", "0x8f", "0x5c", " 0x96", "0x65", "0xc2",
"0x2d", "0x0c", "0x67", " 0xc7"}

It gives me a run-time error Input string was not in a correct format.



Running out of ideas..dont see any other definitons anywhere.



TIA,

HS
 
Harold S said:
Hello,
I am having trouble defining a byte array in .Net


When i go this route it doesnt like the [] definition
Dim xxxx[] As New Bytearray = {"0x45", "0x70", "0x22", "0x48",
"0x9d", "0x17", "0x22", " 0x11", " 0x7f", " 0x96", "0x5f", "0x8f",
"0x5c", " 0x96", "0x65", "0xc2", "0x2d", "0x0c", "0x67", " 0xc7"}



When I go this route

Dim xxx() As Byte = {"0x45", "0x70", "0x22", "0x48", "0x9d", "0x17",
"0x22", " 0x11", " 0x7f", " 0x96", "0x5f", "0x8f", "0x5c", " 0x96",
"0x65", "0xc2", "0x2d", "0x0c", "0x67", " 0xc7"}

It gives me a run-time error Input string was not in a correct
format.

Run-time error? I can not even compile the code. Forgot to enable Option
Strict? Why assign strings to the array? Use numeric values:

Dim xxx() As Byte = {&H45, &h70,&H22, &H48}
 
Thanks Armin...I will give that a try!!
On an added note is there a site where i can find numeric equivalents
eg: for 0x300

Thanks a lot again!
Harry!
 
Harold said:
Hello,
I am having trouble defining a byte array in .Net


When i go this route it doesnt like the [] definition
Dim xxxx[] As New Bytearray = {"0x45", "0x70", "0x22", "0x48", "0x9d",
"0x17", "0x22", " 0x11", " 0x7f", " 0x96", "0x5f", "0x8f", "0x5c", "
0x96", "0x65", "0xc2", "0x2d", "0x0c", "0x67", " 0xc7"}

[] is for C, C++, C#, and certain languages whose name starts with J.
When I go this route

Dim xxx() As Byte = {"0x45", "0x70", "0x22", "0x48", "0x9d", "0x17",
"0x22", " 0x11", " 0x7f", " 0x96", "0x5f", "0x8f", "0x5c", " 0x96",
"0x65", "0xc2", "0x2d", "0x0c", "0x67", " 0xc7"}

That's the correct way of doing it, only you are assigning Strings to a Byte
array. Naturally, it can't cast a String to a Byte. Assign it numbers
instead.
0x is the prefix for hex in C etc., in Visual Basic it's &H.
So your code would become:

Dim xxx() As Byte = {&H45, &H70, &H22, &H48, &H9D, &H17, _
&H22, &H11, &H7F, &H96, &H5F, &H8F, &H5C, &H96, _
&H65, &HC2, &H2D, &HC, &H67, &HC7}
 
Back
Top