Mega Code Archive

 
Categories / VB.Net / Data Types
 

Implements IFormatProvider, ICustomFormatter

Public Class ByteByByteFormatter : Implements IFormatProvider, ICustomFormatter    Public Function GetFormat(formatType As Type) As Object _                    Implements IFormatProvider.GetFormat       If formatType Is GetType(ICustomFormatter) Then          Return Me       Else          Return Nothing       End If    End Function    Public Function Format(fmt As String, arg As Object, _                           formatProvider As IFormatProvider) As String _                           Implements ICustomFormatter.Format       If Not formatProvider.Equals(Me) Then Return Nothing       If Not fmt.StartsWith("X") Then              Return Nothing       End If       If Not typeof arg Is Byte And _          Not typeof arg Is Int16 And _          Not typeof arg Is Int32 And _          Not typeof arg Is Int64 And _          Not typeof arg Is SByte And _          Not typeof arg Is UInt16 And _          Not typeof arg Is UInt32 And _          Not typeof arg Is UInt64 Then _             Return Nothing       Dim bytes() As Byte = BitConverter.GetBytes(arg)       Dim output As String = Nothing       For ctr As Integer = bytes.Length - 1 To 0 Step -1          output += String.Format("{0:X2} ", bytes(ctr))          Next       Return output.Trim()    End Function End Class Public Module Example    Public Sub Main()       Dim value As Byte = 214       Console.WriteLine(String.Format(New ByteByByteFormatter(), "{0,10:N0}", value))    End Sub End Module