UninterpretedBytes.st
changeset 19301 21b0b9bf3a74
parent 19197 641f64b6c686
child 19304 f6490a32d738
child 19345 e2526c51d771
--- a/UninterpretedBytes.st	Sat Mar 05 11:15:22 2016 +0100
+++ b/UninterpretedBytes.st	Mon Mar 07 00:38:41 2016 +0100
@@ -515,7 +515,7 @@
 uint32At:zeroBasedIndex
     "return the 4-bytes starting at index as (unsigned) Integer.
      The index is a C index (i.e. 0-based).
-     The value is retrieved in the machines natural byte order.
+     The value is retrieved in the machine's natural byte order.
      Similar to unsignedLongAt:, except for the index base"
 
     ^ self unsignedLongAt:zeroBasedIndex+1
@@ -631,6 +631,35 @@
      (b unsignedIntegerAt:2 length:4 bigEndian:false).
      (b unsignedIntegerAt:2 length:4 bigEndian:true).
     "
+!
+
+unsignedIntegerAt:index put:newValue length:n bigEndian:bigEndian
+    "store the n-byte unsigned integer starting at index.
+     With n=1, this stores a single byte's value,
+     n=2, an unsigned short, n=4 an unsigned int etc.
+     Useful to replace arbitrary long integers"
+
+    |val|
+
+    val := newValue.
+    bigEndian ifTrue:[
+        index to:index+n-1 do:[:i |
+            self at:i put:(val bitAnd:16rFF).
+            val := val bitShift:-8.
+        ]
+    ] ifFalse:[
+        index+n-1 to:index by:-1 do:[:i |
+            self at:i put:(val bitAnd:16rFF).
+            val := val bitShift:-8.
+        ]
+    ].
+
+    "
+     |b|
+     b := #[ 16r01 16r02 16r03 16r04 16r05 ] copy.
+     (b unsignedIntegerAt:2 put:16r11223344 length:3 bigEndian:false). b.
+     (b unsignedIntegerAt:2 put:16r11223344 length:3 bigEndian:true). b.
+    "
 ! !
 
 !UninterpretedBytes methodsFor:'accessing-bytes'!
@@ -2176,11 +2205,11 @@
 unsignedLongAt:index
     "return the 4-bytes starting at index as an (unsigned) Integer.
      The index is a smalltalk index (i.e. 1-based).
-     The value is retrieved in the machines natural byte order.
+     The value is retrieved in the machine's natural byte order.
      Subclasses may redefine this for better performance.
      Same as doubleWordAt: for protocol completeness"
 
-    ^ self doubleWordAt:index
+    ^ self doubleWordAt:index MSB:IsBigEndian
 
     "
      |b|