|
-
Aug 6th, 2015, 08:26 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Overload ToString for Byte array
Hi folks
First let me say that Im still a newb at VB 
I would like to overload the ToString() method for a byte array, or better yet, overload the Tostring() method for a UInt16 but only within a certain class (i.e. when I call UInt16.ToString withn Foo class it uses my overload, when I call it outside of Foo class it uses the standard method)
First, is the latter possible?
Secondly My reason for wanting to overload this is so that I can have it make a string of the hex values of each byte with some pretty formatting, e.g. if my array of two bytes is {&HA5, &H5A} Then MyArray.ToString will return a string = "5A A5" (Little-endian). Is overloads the way to go? Or an Override, or an Extension?
Thirdly I have tried Overloading as follows but the call in the same class still seems to use the normal ToString meaning it results in the name of the type ("System.Byte[]"), rather than anything useful.
VB Code:
Public Class Foo
Public Sub Bar ()
Dim MyInt As Integer = 42330
Dim MyString As New String(BitConverter.GetBytes(MyInt).ToString) ' Should use my Overload below
Dim BetterString As New String(MyInt.ToString) ' Should be "5A A5" not "42330"
End Sub
Public Overloads Function ToString(Of T)(a As Byte()) As String
Dim RetVal As New System.Text.StringBuilder(a.Length * 3)
Dim ByteStr As String
Dim Last As Integer = a.Length - 1
For i As Integer = 0 To Last
ByteStr = New String(Conversion.Hex(a(i)))
If ByteStr.Length = 1 Then
RetVal.Append("0")
End If
RetVal.Append(ByteStr)
If i <> Last Then
RetVal.Append(" ")
End If
Next
Return RetVal.ToString
End Function
End Class
Public Class Wiz
Public Sub Foo
Dim MyInt As Integer = 42330
Dim MyString As NewString(MyInt.ToString) ' Should always be "42330"
End Sub
End Class
Thanks!
Thanks 
-
Aug 6th, 2015, 09:09 AM
#2
Re: Overload ToString for Byte array
Try doing something like this:
Code:
Public Class Foo
Public Property Hexes As Byte()
Public Overrides Function ToString() As String
Dim RetVal As New System.Text.StringBuilder(Me.Hexes.Length * 3)
Dim ByteStr As String
Dim Last As Integer = Me.Hexes.Length - 1
For i As Integer = 0 To Last
ByteStr = New String(Microsoft.VisualBasic.Conversion.Hex(Me.Hexes(i)))
If ByteStr.Length = 1 Then
RetVal.Append("0")
End If
RetVal.Append(ByteStr)
If i <> Last Then
RetVal.Append(" ")
End If
Next
Return RetVal.ToString
End Function
Public Sub New(ByVal hexValues() As Byte)
Me.Hexes = hexValues
End Sub
End Class
I think that you could make your code inside the ToString more... .NET, but I just don't know enough about it to help. However the code I provided does what you want(I think).
-
Aug 6th, 2015, 09:18 AM
#3
Re: Overload ToString for Byte array
I'd say that you are making this harder than you need to. Consider that if you didn't overload the .ToString method then you would be able to call Foo.ToString and you would get something back...something useless to you, but something. The point is that every object has an implementation of .ToString. An array of byte would have an implementation of .ToString as well, you just appear to want the situation that an array of byte in a certain context would have a different implementation of .ToString, and you appear to be tying yourself into knots to get there. It seems like there are better solutions, though which one would be best is hard to say.
One option would be to create a class that wrapped the array of bytes. This class could overload .ToString however it wanted to, and the context wouldn't make any difference anymore. Wherever you used that class you'd have that overload available. You could give it a constructor that took an array of bytes as an argument, too, so converting a standard array of bytes into an instance of that class would be easy.
Another option might be to create an extension method with a name other than .ToString. It would apply all the time, but so what? What's so special about the name .ToString.
The third option, and likely the easiest, would be to create a function in a module that took an array of bytes as an argument and returned the formatting you wanted. After all, is it really that much different calling:
ToString(myArrayOfBytes)
versus
myArrayOfBytes.ToString
I'm not sure that you can create a method called ToString in a module, because that might cause a conflict, since a module is just a class under the hood, so it should have a .ToString implementation already, but the name could be changed easily enough.
My usual boring signature: Nothing
 
-
Aug 6th, 2015, 09:30 AM
#4
Thread Starter
Hyperactive Member
Re: Overload ToString for Byte array
@dday9:
That would override the Object.ToString() for Foo, allowing a customisation of Foo.ToString(), wouldn't it?
This is different to what I am trying to achieve which is to "subvert" the Integer.ToString() while within Foo
A usage example:
VB Code:
Public Class Foo Private _someInt As integer Public sub New(ByVal i As Integer) _someInt = i End Sub Public Function GetByteString() As String Return _someInt.ToString() End Function Private Overloads Function ToString(i As Integer) As String ' Does not overload Object.ToString for purpose of Foo.ToString, ' but instead overloads Integer.ToString() for Foo.Integer.ToString() use ' ... Byte string conversion and formatting here ... Return TheNewString End Function End Class Public Class MainClass Public Sub Main() Dim X As Integer = 42330 Dim Y as New Foo(42330) Console.WriteLine(X.ToString) ' Prints "42330" Console.WriteLine(Y.GetByteString) ' Prints "5A A5" End Sub End Class
Last edited by wolf99; Aug 6th, 2015 at 10:28 AM.
Thanks 
-
Aug 6th, 2015, 10:42 AM
#5
Re: Overload ToString for Byte array
If that is the case then you should follow Shaggy's third option in post #3. Just create a module to hold various functions that convert some type(or array of type) to your desired string format:
Code:
Public Module LittleEndian
Public Function GetByteString(ByVal values() As Byte) As String
'Algorithm to convert bytes to desired string format
End Function
Public Function GetInt32String(ByVal value As Int32) As String
'Algorithm to convert Int32 to desired string format
End Function
End Module
-
Aug 6th, 2015, 11:07 AM
#6
Thread Starter
Hyperactive Member
Re: Overload ToString for Byte array
I will try that out dday9, and also maybe try the extension option as well, thanks both! 
damnit dday9, thats twice I've tried to squash your signature with my hand!!
Thanks 
-
Aug 6th, 2015, 02:28 PM
#7
Re: [RESOLVED] Overload ToString for Byte array
What's really fun is when you have scrolled one of his posts to the point where that bug just barely wanders into the top of your screen from time to time.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|