By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,695 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,695 IT Pros & Developers. It's quick & easy.

Trouble defining a bytearray!!

P: n/a
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


Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Harold S" <hs****@grange.com> schrieb
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}
--
Armin

Nov 20 '05 #2

P: n/a


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!

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

P: n/a
"Harold Soans" <hs****@yahoo.com> schrieb


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


site? 3 * 256 = 768
You can use calc.exe.
or: msgbox(val("&H300"))
or: msgbox(convert.toint32("300",16))
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4

P: n/a
Harold S wrote:
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}

--
Sven Groot
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.