0

i m working on translating code from VB to C#, though there are lots of great conversion websites, i still find the code ambiguous to me, since the documentation is really poor it approaches to useless, i thought about posting it here to see if i can get it clearer.

i need some elaboration on the code given below, and whats the use of the Buffer[] in the method below:

Function hexToBin(ByVal str As String, ByRef Buffer() As Byte)
  Dim strRemain As String
  Dim firstChar As Boolean
  Dim i, count, inputLen, remainLen As Integer

  i = 0
  count = 0
  firstChar = True
  strRemain = str

  While Len(strRemain) > 0
    If Mid(strRemain, 1, 1) = " " Then
      firstChar = True
      strRemain = Mid(strRemain, 2)
    ElseIf firstChar = True Then
      If Len(strRemain) = 1 Then
        Buffer(count) = myVal(strRemain)
      ElseIf Len(strRemain) >= 2 Then
        Buffer(count) = myVal(Mid(strRemain, 1, 1)) * 16 + myVal(Mid(strRemain, 2, 1))

        strRemain = Mid(strRemain, 3)
      End If

      count = count + 1
      firstChar = False
    Else
      strRemain = Mid(strRemain, 2)
    End If
  Wend

  hexToBin = count
End Function

see, i know this code converts from hex to binary as the name suggests, yet i cant really figure the use of the Buffer[] in the context, i looked up the Mid function in string VB, but still can't figure out the use of the Buffer[] in this function, i would appreciate if someone explained the use of the buffer.

Samuel
  • 6,126
  • 35
  • 70
Ameer Adel
  • 82
  • 1
  • 1
  • 12
  • `buffer` appears to be the return. the proper way to do that would be: `hexToBin(str As String) As Byte()` and create/return the buffer as a function return rather than passing one to be filled. – Ňɏssa Pøngjǣrdenlarp Jan 04 '15 at 13:33
  • Is this visual basic or vb.net? – SQLDiver Jan 04 '15 at 13:39
  • @SQLDiver Visual Basic – Ameer Adel Jan 04 '15 at 13:50
  • Be aware that vb.net is not backwards compatible with Visual Basic. - this may cause issues when translating to any CLR language. My reason for highlighting this is that there is a CLR Buffer class, which may make you consider renaming this parameter. – SQLDiver Jan 04 '15 at 13:57
  • It is incredibly poor code, throw it away. – Hans Passant Jan 04 '15 at 13:58
  • @Plutonix: whoever wrote this is "aping" the way that the Win32 API handles strings. You send in a string pointer as an argument and the API populates it, right? So this is an "I know my way around the API" statement. While the "proper" way to do this (currently) is to use a return value, this was probably viewed as the height of sophistication back in 1991 when this code was probably written. I can see the writer turning his nose up at you and Hans here, saying that you "obviously" didn't understand the deeper principles of VB, and walking away. Some things never change, after all. :) – BobRodes Jan 04 '15 at 14:38
  • @BobRodes thanks for your precise and colorful imagination, but i believe that Plutonix, mentioned the fact its being used as a return byte. – Ameer Adel Jan 04 '15 at 15:08
  • Yes he did. A little further information is that the way the WinAPI populates the string argument is much in the same way that this code populates the byte array. The string argument in the WinAPI is actually a LPTSTR argument, which is a pointer to where a string commences in memory. The API pushes bytes into memory starting at that pointer location, and puts a null value when finished. The caller then can poll the memory location for the data. This is pretty much analogous to what the coder is doing with his byte array. – BobRodes Jan 04 '15 at 15:31
  • you seem to be advanced in API, impressive. – Ameer Adel Jan 04 '15 at 15:56
  • Thank you. I would characterize my understanding of the API as intermediate, in that I know some things well and others not so well. For example, "pushes" is a bad word here, since it implies some sort of LIFO arrangement, which is inaccurate. Perhaps "sticks" is a better word. :) – BobRodes Jan 06 '15 at 06:06
  • p. s. If you want to use this Buffer idea in C#, you would use ref or out. For more information see [this](http://msdn.microsoft.com/en-us/library/vstudio/szasx730(v=vs.100).aspx). – BobRodes Jan 06 '15 at 06:15

1 Answers1

2

In the code above, the parentheses are used to access elements of an array. So, Buffer(i) refers to the ith element of the array Buffer.

You can learn more about arrays in VB from any text book, or indeed from MSDN: http://msdn.microsoft.com/en-us/library/wak0wfyt.aspx

As for why the array Buffer is used in the first place, well that's to store the output of the function. The function takes a hex string as input and populates the byte array Buffer with the binary equivalent.

This does seem to be rather inefficient code though. And it presents a somewhat clumsy interface because it asks the caller to allocate the array. Rather than translating it, I think I would start here: How can I convert a hex string to a byte array?

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    I think the OP is asking _why_ a buffer is used. – Jason Faulkner Jan 04 '15 at 13:29
  • yes, thanks david, after reading the function for couple of times it hit me that i will rewrite it rather than translate it, but i was confused about the buffer, which you guys have explained is a return byte array. thanks again – Ameer Adel Jan 04 '15 at 13:54