@@ -80,7 +80,8 @@ public enum Representation
8080 {
8181 Long ,
8282 Double ,
83- ByteArray
83+ ByteArray ,
84+ ULong
8485 }
8586
8687 public const long MinValueChar = 0x20 ;
@@ -121,17 +122,18 @@ public enum Representation
121122
122123 public const float MinValueFloat = float . Epsilon ;
123124 public const float MaxValueFloat = float . MaxValue ;
124- public const float NullValueFloat = float . NaN ; // TODO: can NOT be used as a normal equality check
125+ public const float NullValueFloat = float . NaN ;
125126
126127 public const double MinValueDouble = double . Epsilon ;
127128 public const double MaxValueDouble = double . MaxValue ;
128- public const double NullValueDouble = double . NaN ; // TODO: can NOT be used as a normal equality check
129+ public const double NullValueDouble = double . NaN ;
129130
130131 private readonly byte [ ] _byteArrayValue ;
131132 private readonly byte [ ] _byteArrayValueForLong = new byte [ 1 ] ;
132133 private readonly string _characterEncoding ;
133134 private readonly double _doubleValue ;
134135 private readonly long _longValue ;
136+ private readonly ulong _unsignedLongValue ;
135137 private readonly Representation _representation ;
136138 private readonly int _size ;
137139
@@ -145,6 +147,7 @@ public PrimitiveValue(long value, int size)
145147 _representation = Representation . Long ;
146148 _longValue = value ;
147149 _doubleValue = 0.0 ;
150+ _unsignedLongValue = 0 ;
148151 _byteArrayValue = null ;
149152 _characterEncoding = null ;
150153 _size = size ;
@@ -159,12 +162,29 @@ public PrimitiveValue(double value, int size)
159162 {
160163 _representation = Representation . Double ;
161164 _longValue = 0 ;
165+ _unsignedLongValue = 0 ;
162166 _doubleValue = value ;
163167 _byteArrayValue = null ;
164168 _characterEncoding = null ;
165169 _size = size ;
166170 }
167171
172+ /// <summary>
173+ /// Construct and fill in value as a double.
174+ /// </summary>
175+ /// <param name="value"> in double format </param>
176+ /// <param name="size"></param>
177+ public PrimitiveValue ( ulong value , int size )
178+ {
179+ _representation = Representation . ULong ;
180+ _unsignedLongValue = 0 ;
181+ _longValue = 0 ;
182+ _doubleValue = 0 ;
183+ _byteArrayValue = null ;
184+ _characterEncoding = null ;
185+ _size = size ;
186+ }
187+
168188 /// <summary>
169189 /// Construct and fill in value as a byte array.
170190 /// </summary>
@@ -176,6 +196,7 @@ public PrimitiveValue(byte[] value, string characterEncoding, int size)
176196 _representation = Representation . ByteArray ;
177197 _longValue = 0 ;
178198 _doubleValue = 0.0 ;
199+ _unsignedLongValue = 0 ;
179200 _byteArrayValue = value ;
180201 _characterEncoding = characterEncoding ;
181202 _size = size ;
@@ -217,30 +238,28 @@ public static PrimitiveValue Parse(string value, PrimitiveType primitiveType)
217238 return new PrimitiveValue ( byte . Parse ( value ) , 1 ) ;
218239
219240 case SbePrimitiveType . Int8 :
220- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 1 ) ;
241+ return new PrimitiveValue ( Convert . ToSByte ( value ) , 1 ) ;
221242
222243 case SbePrimitiveType . Int16 :
223- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 2 ) ;
244+ return new PrimitiveValue ( Convert . ToInt16 ( value ) , 2 ) ;
224245
225246 case SbePrimitiveType . Int32 :
226- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 4 ) ;
247+ return new PrimitiveValue ( Convert . ToInt32 ( value ) , 4 ) ;
227248
228249 case SbePrimitiveType . Int64 :
229250 return new PrimitiveValue ( Convert . ToInt64 ( value ) , 8 ) ;
230251
231252 case SbePrimitiveType . UInt8 :
232- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 1 ) ;
253+ return new PrimitiveValue ( Convert . ToByte ( value ) , 1 ) ;
233254
234255 case SbePrimitiveType . UInt16 :
235- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 2 ) ;
256+ return new PrimitiveValue ( Convert . ToUInt16 ( value ) , 2 ) ;
236257
237258 case SbePrimitiveType . UInt32 :
238- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 4 ) ;
259+ return new PrimitiveValue ( Convert . ToUInt32 ( value ) , 4 ) ;
239260
240261 case SbePrimitiveType . UInt64 :
241- // TODO: not entirely adequate, but then again, Java doesn't have unsigned 64-bit integers...
242- // TODO to fix in .NET
243- return new PrimitiveValue ( Convert . ToInt64 ( value ) , 8 ) ;
262+ return new PrimitiveValue ( Convert . ToUInt64 ( value ) , 8 ) ;
244263
245264 case SbePrimitiveType . Float :
246265 return new PrimitiveValue ( Convert . ToDouble ( value ) , 4 ) ;
@@ -267,6 +286,20 @@ public long LongValue()
267286 return _longValue ;
268287 }
269288
289+ /// <summary>
290+ /// Return unsigned long value for this PrimitiveValue
291+ /// </summary>
292+ /// <returns>value expressed as a ulong</returns>
293+ public ulong ULongValue ( )
294+ {
295+ if ( _representation != Representation . ULong )
296+ {
297+ throw new InvalidOperationException ( "PrimitiveValue is not a ulong representation" ) ;
298+ }
299+
300+ return _unsignedLongValue ;
301+ }
302+
270303 /// <summary>
271304 /// Return double value for this PrimitiveValue.
272305 /// </summary>
@@ -325,6 +358,9 @@ public override string ToString()
325358 case Representation . Long :
326359 return Convert . ToString ( _longValue ) ;
327360
361+ case Representation . ULong :
362+ return Convert . ToString ( _longValue ) ;
363+
328364 case Representation . Double :
329365 return Convert . ToString ( _doubleValue ) ;
330366
@@ -354,6 +390,9 @@ public override bool Equals(object value)
354390 case Representation . Long :
355391 return _longValue == rhs . _longValue ;
356392
393+ case Representation . ULong :
394+ return _unsignedLongValue == rhs . _unsignedLongValue ;
395+
357396 case Representation . Double :
358397 return _doubleValue == rhs . _doubleValue ;
359398
@@ -377,6 +416,9 @@ public override int GetHashCode()
377416 case Representation . Long :
378417 return _longValue . GetHashCode ( ) ;
379418
419+ case Representation . ULong :
420+ return _unsignedLongValue . GetHashCode ( ) ;
421+
380422 case Representation . Double :
381423 return _doubleValue . GetHashCode ( ) ;
382424
0 commit comments