1

I have a byte array need to convert to integer, and this array only have one value. I tried Bitconverter, convert.ToInt32 both are not working for me. my code as follows:

Dim a As new Byte() ={&H1C} ' the value range is {&H01} to {&HFF}
Dim key As integer = BitConverter.ToInt32(a,1)

I need the result with key = 28, which convert function I should use? Thank you very much.

2 Answers 2

1

BitConverter.ToInt32 needs 4 bytes to work with, so you just need to put your one byte value into a 4 byte array. Allowing for the endianness, something like this:

Dim a() As Byte = { &H1C }
Dim b(3) As Byte
If BitConverter.IsLittleEndian Then
    b(0) = a(0)
Else
    b(3) = a(0)
End If
Dim key As Integer = BitConverter.ToInt32(b, 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Hi@Mark, thanks for your answer, I tested it, and kind like not working for me, finally, I use CInt() function to do this part. Thank you.
1

You are not converting an array of values, but rather a single array element.

That said, there is no need to call a conversion function to convert a single Byte to an Integer. Just assign the value.

Dim key As Integer = a(0)

2 Comments

Haha, simpler is better... now I feel like a dummy for my answer!
@mark, honestly this whole question should be deleted. I only posted this after reading the OP's comment to your answer and wanted see if I could trigger some brain cell's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.