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
Related examples in the same category
1. | Standard Numeric Format Strings: C2 | | |
2. | Standard Numeric Format Strings: C, C3 | | |
3. | Standard Numeric Format Strings: D | | |
4. | Standard Numeric Format Strings for negative value: D | | |
5. | Standard Numeric Format Strings: E,e | | |
6. | Standard Numeric Format Strings: F | | |
7. | Standard Numeric Format Strings: G | | |
8. | Standard Numeric Format Strings: N | | |
9. | Standard Numeric Format Strings: N | | |
10. | Standard Numeric Format Strings: P | | |
11. | Standard Numeric Format Strings: r | | |
12. | Standard Numeric Format Strings: X, x | | |
13. | Custom double value format: 00000 | | |
14. | Custom double value format: 0.00 | | |
15. | Custom double value format: 00.00 | | |
16. | Custome double value format: 00.00 and CultureInfo.CreateSpecificCulture("da-DK") | | |
17. | Double value format: 0.0 | | |
18. | Double value format: #,# | | |
19. | Double value format: #,# and CultureInfo.CreateSpecificCulture("el-GR") | | |
20. | Custome number format: ToString("#.##", CultureInfo.InvariantCulture) | | |
21. | Double value format: ToString("[##-##-##]") | | |
22. | Double value format: ToString("#") | | |
23. | Double value format: (###) ###-#### | | |
24. | Number format: #,# | | |
25. | Number format: #,##0,, | | |
26. | Number format: #,, | | |
27. | Number format: #,,, | | |
28. | Number format: #,##0,, | | |
29. | Double number format: "#0.##%" | | |
30. | Format with custom character | | |
31. | Double value format: 0.###E+0 | | |
32. | Double value format: 0.###E+000 | | |
33. | Double value format: 0.###E-000 | | |
34. | Format escape: \# | | |
35. | Format escape: \\(back slash) | | |
36. | Positive double value format: ##;(##) | | |
37. | Negative double value format: ##;(##) | | |
38. | Double value format: ##;(##);**Zero** | | |
39. | Double value format: (###) ###-#### | | |
40. | Integer format: # | | |
41. | Number format: 0.0 vs #,# | | |
42. | Number format: ###-#### | | |
43. | Number format: 0 and # | | |
44. | Number format: 0, # and , | | |
45. | Number format: #,,, | | |
46. | Number format: #0.##% | | |
47. | Number format: CultureInfo.InvariantCulture | | |
48. | Number format: E | | |
49. | Number format: escape | | |
50. | Number format: free text | | |
51. | Number format: phone number format | | |
52. | Add free text to number format | | |
53. | Double.ToString Method and Number format | | |
54. | Number format and Culture info:G | | |
55. | Number format and Culture info: C | | |
56. | Number format and Culture info:E04 | | |
57. | Number format and Culture info:F | | |
58. | Number format and Culture info:N | | |
59. | Number format and Culture info:P | | |
60. | Converts string in style and culture-specific format to double | | |
61. | Integer format: X | | |
62. | Format Enum: "G", "F", "D", "X" | | |
63. | Byte value format: X4 | | |
64. | Date format: D | | |
65. | Custom format with ToString | | |
66. | Format Date value: %M | | |
67. | Date value format: MMMM dd, yyyy (dddd) | | |
68. | Decimal format: C3 | | |