Skip to content

Commit edab5f1

Browse files
CyDragon80dcodeIO
authored andcommitted
Added fromBytes (dcodeIO#43)
* Added fromBytes. Also add entries for fromBytes/toBytes to README. * Name the anon functions in toBytes to match previous. Put doc entries in order.
1 parent 8cdc1f7 commit edab5f1

File tree

8 files changed

+172
-28
lines changed

8 files changed

+172
-28
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ assumed to use 32 bits.
127127
| unsigned | *boolean* | Whether unsigned or not, defaults to `false` for signed
128128
| **@returns** | *!Long* | The corresponding Long value
129129

130+
#### Long.fromBytes(arrBytes, offset=, unsigned=, le=)
131+
132+
Returns a Long representing the 64 bit integer that comes by concatenating the given bytes.
133+
134+
|Parameter |Type |Description
135+
|---------------|---------------|-------------
136+
|arrBytes |*!Array.<number>*|Byte representation in an array of at least offset+8 bytes
137+
|offset |*number* |The starting index from which to read 8 elements of the array, defaults to zero
138+
|unsigned |*boolean* |Whether unsigned or not, defaults to `false` for signed
139+
|le |*boolean* |Whether little or big endian, defaults to big endian
140+
|**@returns** |*!Long* |The corresponding Long value
141+
130142
#### Long.fromInt(value, unsigned=)
131143

132144
Returns a Long representing the given 32 bit integer value.
@@ -451,6 +463,15 @@ Returns the difference of this and the specified Long.
451463
| subtrahend | *!Long | number | string* | Subtrahend
452464
| **@returns** | *!Long* | Difference
453465

466+
#### Long#toBytes(le=)
467+
468+
Converts this Long to its byte representation.
469+
470+
|Parameter |Type |Description
471+
|---------------|---------------|-------------
472+
|le |*boolean* |Whether little or big endian, defaults to big endian
473+
|**@returns** |*!Array.<number>*|Byte representation
474+
454475
#### Long#toInt()
455476

456477
Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
@@ -502,6 +523,8 @@ Returns the bitwise XOR of this Long and the given one.
502523
| other | *!Long | number | string* | Other Long
503524
| **@returns** | *!Long* |
504525

526+
527+
505528
Downloads
506529
---------
507530
* [Distributions](https://github.com/dcodeIO/long.js/tree/master/dist)

dist/long.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,15 @@
11631163
* @param {boolean=} le Whether little or big endian, defaults to big endian
11641164
* @returns {!Array.<number>} Byte representation
11651165
*/
1166-
LongPrototype.toBytes = function(le) {
1166+
LongPrototype.toBytes = function toBytes(le) {
11671167
return le ? this.toBytesLE() : this.toBytesBE();
1168-
}
1168+
};
11691169

11701170
/**
11711171
* Converts this Long to its little endian byte representation.
11721172
* @returns {!Array.<number>} Little endian byte representation
11731173
*/
1174-
LongPrototype.toBytesLE = function() {
1174+
LongPrototype.toBytesLE = function toBytesLE() {
11751175
var hi = this.high,
11761176
lo = this.low;
11771177
return [
@@ -1184,13 +1184,13 @@
11841184
(hi >>> 16) & 0xff,
11851185
(hi >>> 24) & 0xff
11861186
];
1187-
}
1187+
};
11881188

11891189
/**
11901190
* Converts this Long to its big endian byte representation.
11911191
* @returns {!Array.<number>} Big endian byte representation
11921192
*/
1193-
LongPrototype.toBytesBE = function() {
1193+
LongPrototype.toBytesBE = function toBytesBE() {
11941194
var hi = this.high,
11951195
lo = this.low;
11961196
return [
@@ -1203,7 +1203,51 @@
12031203
(lo >>> 8) & 0xff,
12041204
lo & 0xff
12051205
];
1206+
};
1207+
1208+
/**
1209+
* @param {!Array.<number>} arrBytes
1210+
* @param {number=} offset
1211+
* @param {boolean=} unsigned
1212+
* @param {boolean=} le
1213+
* @returns {!Long}
1214+
* @inner
1215+
*/
1216+
function fromBytes(arrBytes, offset, unsigned, le) {
1217+
if (typeof(offset)!=='number') offset=0;
1218+
if (le) {
1219+
var lo = arrBytes[offset++];
1220+
lo |= (arrBytes[offset++] << 8);
1221+
lo |= (arrBytes[offset++] << 16);
1222+
lo |= (arrBytes[offset++] << 24);
1223+
var hi = arrBytes[offset++];
1224+
hi |= (arrBytes[offset++] << 8);
1225+
hi |= (arrBytes[offset++] << 16);
1226+
hi |= (arrBytes[offset] << 24);
1227+
}
1228+
else {
1229+
var hi = (arrBytes[offset++] << 24);
1230+
hi |= (arrBytes[offset++] << 16);
1231+
hi |= (arrBytes[offset++] << 8);
1232+
hi |= arrBytes[offset++];
1233+
var lo = (arrBytes[offset++] << 24);
1234+
lo |= (arrBytes[offset++] << 16);
1235+
lo |= (arrBytes[offset++] << 8);
1236+
lo |= arrBytes[offset];
1237+
}
1238+
return Long.fromBits(lo, hi, unsigned);
12061239
}
12071240

1241+
/**
1242+
* Returns a Long representing the 64 bit integer that comes by concatenating the given bytes.
1243+
* @function
1244+
* @param {!Array.<number>} arrBytes Byte representation in an array of at least offset+8 bytes
1245+
* @param {number=} offset The starting index from which to read 8 elements of the array, defaults to zero
1246+
* @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed
1247+
* @param {boolean=} le Whether little or big endian, defaults to big endian
1248+
* @returns {!Long} The corresponding Long value
1249+
*/
1250+
Long.fromBytes = fromBytes;
1251+
12081252
return Long;
12091253
});

0 commit comments

Comments
 (0)