Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21096

Concat Byte Arrays via String - Reliable?

$
0
0
While this isn't the best way to go about combining Byte data for processing, it is quick and dirty, good enough for many jobs, and as far as I know is always reliable.

At least I thought it was.

I am writing some code that performs some complex hashing. In order to focus on just getting the algorithm properly implemented I am relying on fiddling with Byte data via String variables.

I thought I understood that something like:

Code:

Dim S as String
Dim B() As Byte

...

S = B

... copied the bytes from B losslessy into S with no truncation and no padding. Further:

Code:

B = S
.. should do the same in reverse.

No padding to a "Unicode" String should occur and no truncation of an odd-length Byte array should occur even when concatenating:

Code:

B = S & StrConv("123", vbFromUnicode)

Do you see anything wrong with doing this? I can't, but my hashing doesn't produce the expected results and I can't see where else I might be going wrong.

If the process described above is good (if slow) I might post my hashing code for assistance. But I was trying to spare anyone the trouble of wading throught it. It's a bit complicated.


Here's a demo of the String/Byte stuff:

Code:

Option Explicit

'Concatenate Byte arrays via String operations.
'
'The Byte arrays contain 7-bit ASCII data (actually ANSI but
'we'll use only values in the 0-127 range).  We convert from
'String data vis StrConv() calls.
'
'Convert back to Unicode to "dump" the results for viewing.

Private Sub DumpByteArray(ByRef BA() As Byte)
    Text5.Text = "Length = " & CStr(UBound(BA) + 1) _
              & "  Value = [" & StrConv(BA, vbUnicode) & "]"
End Sub

Private Sub Command1_Click()
    Dim B1() As Byte
    Dim B4() As Byte
    Dim S1 As String
    Dim S4 As String
    Dim Result() As Byte

    B1 = StrConv(Text1.Text, vbFromUnicode)
    S1 = B1
    B4 = StrConv(Text4.Text, vbFromUnicode)
    S4 = B4

    Result = S1 _
          & StrConv(Text2.Text _
                  & Text3.Text, _
                    vbFromUnicode) _
          & S4

    DumpByteArray Result
End Sub

Name:  sshot.png
Views: 65
Size:  10.0 KB
Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 21096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>