Skip to content

Commit

Permalink
Added fromBytes (#43)
Browse files Browse the repository at this point in the history
* Added fromBytes. Also add entries for fromBytes/toBytes to README.

* Name the anon functions in toBytes to match previous. Put doc entries in order.
  • Loading branch information
CyDragon80 authored and dcodeIO committed Feb 2, 2018
1 parent 8cdc1f7 commit edab5f1
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 28 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ assumed to use 32 bits.
| unsigned | *boolean* | Whether unsigned or not, defaults to `false` for signed
| **@returns** | *!Long* | The corresponding Long value

#### Long.fromBytes(arrBytes, offset=, unsigned=, le=)

Returns a Long representing the 64 bit integer that comes by concatenating the given bytes.

|Parameter |Type |Description
|---------------|---------------|-------------
|arrBytes |*!Array.<number>*|Byte representation in an array of at least offset+8 bytes
|offset |*number* |The starting index from which to read 8 elements of the array, defaults to zero
|unsigned |*boolean* |Whether unsigned or not, defaults to `false` for signed
|le |*boolean* |Whether little or big endian, defaults to big endian
|**@returns** |*!Long* |The corresponding Long value

#### Long.fromInt(value, unsigned=)

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

#### Long#toBytes(le=)

Converts this Long to its byte representation.

|Parameter |Type |Description
|---------------|---------------|-------------
|le |*boolean* |Whether little or big endian, defaults to big endian
|**@returns** |*!Array.<number>*|Byte representation

#### Long#toInt()

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



Downloads
---------
* [Distributions](https://github.com/dcodeIO/long.js/tree/master/dist)
Expand Down
54 changes: 49 additions & 5 deletions dist/long.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,15 +1163,15 @@
* @param {boolean=} le Whether little or big endian, defaults to big endian
* @returns {!Array.<number>} Byte representation
*/
LongPrototype.toBytes = function(le) {
LongPrototype.toBytes = function toBytes(le) {
return le ? this.toBytesLE() : this.toBytesBE();
}
};

/**
* Converts this Long to its little endian byte representation.
* @returns {!Array.<number>} Little endian byte representation
*/
LongPrototype.toBytesLE = function() {
LongPrototype.toBytesLE = function toBytesLE() {
var hi = this.high,
lo = this.low;
return [
Expand All @@ -1184,13 +1184,13 @@
(hi >>> 16) & 0xff,
(hi >>> 24) & 0xff
];
}
};

/**
* Converts this Long to its big endian byte representation.
* @returns {!Array.<number>} Big endian byte representation
*/
LongPrototype.toBytesBE = function() {
LongPrototype.toBytesBE = function toBytesBE() {
var hi = this.high,
lo = this.low;
return [
Expand All @@ -1203,7 +1203,51 @@
(lo >>> 8) & 0xff,
lo & 0xff
];
};

/**
* @param {!Array.<number>} arrBytes
* @param {number=} offset
* @param {boolean=} unsigned
* @param {boolean=} le
* @returns {!Long}
* @inner
*/
function fromBytes(arrBytes, offset, unsigned, le) {
if (typeof(offset)!=='number') offset=0;
if (le) {
var lo = arrBytes[offset++];
lo |= (arrBytes[offset++] << 8);
lo |= (arrBytes[offset++] << 16);
lo |= (arrBytes[offset++] << 24);
var hi = arrBytes[offset++];
hi |= (arrBytes[offset++] << 8);
hi |= (arrBytes[offset++] << 16);
hi |= (arrBytes[offset] << 24);
}
else {
var hi = (arrBytes[offset++] << 24);
hi |= (arrBytes[offset++] << 16);
hi |= (arrBytes[offset++] << 8);
hi |= arrBytes[offset++];
var lo = (arrBytes[offset++] << 24);
lo |= (arrBytes[offset++] << 16);
lo |= (arrBytes[offset++] << 8);
lo |= arrBytes[offset];
}
return Long.fromBits(lo, hi, unsigned);
}

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

return Long;
});
Loading

0 comments on commit edab5f1

Please sign in to comment.