@@ -45,62 +45,62 @@ public static string GetHexFromInteger(in byte s, bool hexPrefix = false)
45
45
{
46
46
Span < byte > bytes = stackalloc byte [ sizeof ( byte ) ] ;
47
47
bytes [ 0 ] = s ;
48
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
48
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
49
49
}
50
50
51
51
public static string GetHexFromInteger ( in sbyte s , bool hexPrefix = false )
52
52
{
53
53
Span < byte > bytes = stackalloc byte [ sizeof ( sbyte ) ] ;
54
54
bytes [ 0 ] = unchecked ( ( byte ) s ) ;
55
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
55
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
56
56
}
57
57
58
58
public static string GetHexFromInteger ( in short s , bool hexPrefix = false )
59
59
{
60
60
Span < byte > bytes = stackalloc byte [ sizeof ( short ) ] ;
61
61
BinaryPrimitives . WriteInt16BigEndian ( bytes , s ) ;
62
- StripLeadingZeros ( ref bytes ) ;
63
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
62
+ StripLeadingZeroBytes ( ref bytes ) ;
63
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
64
64
}
65
65
66
66
public static string GetHexFromInteger ( in ushort s , bool hexPrefix = false )
67
67
{
68
68
Span < byte > bytes = stackalloc byte [ sizeof ( ushort ) ] ;
69
69
BinaryPrimitives . WriteUInt16BigEndian ( bytes , s ) ;
70
- StripLeadingZeros ( ref bytes ) ;
71
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
70
+ StripLeadingZeroBytes ( ref bytes ) ;
71
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
72
72
}
73
73
74
74
public static string GetHexFromInteger ( in int s , bool hexPrefix = false )
75
75
{
76
76
Span < byte > bytes = stackalloc byte [ sizeof ( int ) ] ;
77
77
BinaryPrimitives . WriteInt32BigEndian ( bytes , s ) ;
78
- StripLeadingZeros ( ref bytes ) ;
79
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
78
+ StripLeadingZeroBytes ( ref bytes ) ;
79
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
80
80
}
81
81
82
82
public static string GetHexFromInteger ( in uint s , bool hexPrefix = false )
83
83
{
84
84
Span < byte > bytes = stackalloc byte [ sizeof ( uint ) ] ;
85
85
BinaryPrimitives . WriteUInt32BigEndian ( bytes , s ) ;
86
- StripLeadingZeros ( ref bytes ) ;
87
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
86
+ StripLeadingZeroBytes ( ref bytes ) ;
87
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
88
88
}
89
89
90
90
public static string GetHexFromInteger ( in long s , bool hexPrefix = false )
91
91
{
92
92
Span < byte > bytes = stackalloc byte [ sizeof ( long ) ] ;
93
93
BinaryPrimitives . WriteInt64BigEndian ( bytes , s ) ;
94
- StripLeadingZeros ( ref bytes ) ;
95
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
94
+ StripLeadingZeroBytes ( ref bytes ) ;
95
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
96
96
}
97
97
98
98
public static string GetHexFromInteger ( in ulong s , bool hexPrefix = false )
99
99
{
100
100
Span < byte > bytes = stackalloc byte [ sizeof ( ulong ) ] ;
101
101
BinaryPrimitives . WriteUInt64BigEndian ( bytes , s ) ;
102
- StripLeadingZeros ( ref bytes ) ;
103
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
102
+ StripLeadingZeroBytes ( ref bytes ) ;
103
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
104
104
}
105
105
106
106
public static string GetHexFromInteger ( in UInt256 s , bool hexPrefix = false )
@@ -115,11 +115,11 @@ public static string GetHexFromInteger(in UInt256 s, bool hexPrefix = false)
115
115
bytes . Reverse ( ) ;
116
116
}
117
117
118
- StripLeadingZeros ( ref bytes ) ;
119
- return GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
118
+ StripLeadingZeroBytes ( ref bytes ) ;
119
+ return GetZeroStrippedHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
120
120
}
121
121
122
- static void StripLeadingZeros ( ref Span < byte > span )
122
+ static void StripLeadingZeroBytes ( ref Span < byte > span )
123
123
{
124
124
int startIndex = 0 ;
125
125
for ( ; startIndex < span . Length && span [ startIndex ] == 0x0 ; startIndex ++ ) { }
@@ -129,6 +129,33 @@ static void StripLeadingZeros(ref Span<byte> span)
129
129
}
130
130
}
131
131
132
+
133
+ static string GetZeroStrippedHexFromBytes ( ReadOnlySpan < byte > bytes , bool hexPrefix )
134
+ {
135
+ // Strips a leading 0 hex char from a hex sequence.
136
+ // Example: 0x0fff -> 0xfff
137
+ // This is a stupid work-around for geth which throws exceptions trying to parse an integer from (valid) hex string
138
+ // that begins has a single 0 after the 0x prefix.
139
+ var hex = GetHexFromBytes ( bytes , hexPrefix : hexPrefix ) ;
140
+ if ( hexPrefix )
141
+ {
142
+ if ( hex . Length > 2 && hex [ 2 ] == '0' )
143
+ {
144
+ hex = "0x" + hex . Substring ( 3 ) ;
145
+ }
146
+ }
147
+ else
148
+ {
149
+ if ( hex . Length > 0 && hex [ 0 ] == '0' )
150
+ {
151
+ hex = hex . Substring ( 1 ) ;
152
+ }
153
+ }
154
+
155
+ return hex ;
156
+ }
157
+
158
+
132
159
public static string GetHex < T > ( in T s , bool hexPrefix = false ) where T : unmanaged
133
160
{
134
161
Span < T > span = stackalloc T [ 1 ]
0 commit comments