How to split large integer into an array of 8-bit integers
Wondering how to convert the output of arbitrarily sized integers like 1
or 12345
or 5324617851000199519157
to an array of integers.
[1] // for the first one
// [probably just a few values for the second 12345...]
[1, 123, 255, 32, ...] // not sure here...
I am not sure what the resulting value would look like or how to compute it, but somehow it would be something like:
A bunch of 8-bit numbers that can be used to reconstruct (somehow) the original arbitrary integer. I am not sure what calculations would be required to do this either. But all I do know is that each unique arbitrarily-sized integer should result in a unique array of 8-bit values. That is, no two different date integers should result in the same array.
It doesn't matter the language much how this is implemented, but probably an imperative language like JavaScript or C.
I am pretty sure the arrays should all be the same length as well, but if that's not possible then knowing how to do it a different way would be okay.
javascript c integer
add a comment |
Wondering how to convert the output of arbitrarily sized integers like 1
or 12345
or 5324617851000199519157
to an array of integers.
[1] // for the first one
// [probably just a few values for the second 12345...]
[1, 123, 255, 32, ...] // not sure here...
I am not sure what the resulting value would look like or how to compute it, but somehow it would be something like:
A bunch of 8-bit numbers that can be used to reconstruct (somehow) the original arbitrary integer. I am not sure what calculations would be required to do this either. But all I do know is that each unique arbitrarily-sized integer should result in a unique array of 8-bit values. That is, no two different date integers should result in the same array.
It doesn't matter the language much how this is implemented, but probably an imperative language like JavaScript or C.
I am pretty sure the arrays should all be the same length as well, but if that's not possible then knowing how to do it a different way would be okay.
javascript c integer
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39
add a comment |
Wondering how to convert the output of arbitrarily sized integers like 1
or 12345
or 5324617851000199519157
to an array of integers.
[1] // for the first one
// [probably just a few values for the second 12345...]
[1, 123, 255, 32, ...] // not sure here...
I am not sure what the resulting value would look like or how to compute it, but somehow it would be something like:
A bunch of 8-bit numbers that can be used to reconstruct (somehow) the original arbitrary integer. I am not sure what calculations would be required to do this either. But all I do know is that each unique arbitrarily-sized integer should result in a unique array of 8-bit values. That is, no two different date integers should result in the same array.
It doesn't matter the language much how this is implemented, but probably an imperative language like JavaScript or C.
I am pretty sure the arrays should all be the same length as well, but if that's not possible then knowing how to do it a different way would be okay.
javascript c integer
Wondering how to convert the output of arbitrarily sized integers like 1
or 12345
or 5324617851000199519157
to an array of integers.
[1] // for the first one
// [probably just a few values for the second 12345...]
[1, 123, 255, 32, ...] // not sure here...
I am not sure what the resulting value would look like or how to compute it, but somehow it would be something like:
A bunch of 8-bit numbers that can be used to reconstruct (somehow) the original arbitrary integer. I am not sure what calculations would be required to do this either. But all I do know is that each unique arbitrarily-sized integer should result in a unique array of 8-bit values. That is, no two different date integers should result in the same array.
It doesn't matter the language much how this is implemented, but probably an imperative language like JavaScript or C.
I am pretty sure the arrays should all be the same length as well, but if that's not possible then knowing how to do it a different way would be okay.
javascript c integer
javascript c integer
edited Nov 20 '18 at 23:32
RobG
97k19103145
97k19103145
asked Nov 20 '18 at 22:30
Lance Pollard
31.1k67204340
31.1k67204340
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39
add a comment |
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39
add a comment |
2 Answers
2
active
oldest
votes
Most languages, including C and Javascript, have bit-shifting and bit-masking operations as part of their basic math operations. But beware Javascript: numbers are 64 bits, but only 32-bit masking operations are allowed. So:
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in thebytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from0
to'47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.
– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand whereMath.floor(bignum / 0x100000000)
comes from?
– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
add a comment |
I'm not sure if this is too brute-forcey for what you want, but you can take an arbitrary string and just do the long division into a unit8Array
.
Here's a function (borrowed liberally from here) that will convert back and forth from an arbitrarily long string:
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
No doubt, there are some efficiencies to be found (maybe you can avoid the interim arrays).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402560%2fhow-to-split-large-integer-into-an-array-of-8-bit-integers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Most languages, including C and Javascript, have bit-shifting and bit-masking operations as part of their basic math operations. But beware Javascript: numbers are 64 bits, but only 32-bit masking operations are allowed. So:
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in thebytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from0
to'47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.
– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand whereMath.floor(bignum / 0x100000000)
comes from?
– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
add a comment |
Most languages, including C and Javascript, have bit-shifting and bit-masking operations as part of their basic math operations. But beware Javascript: numbers are 64 bits, but only 32-bit masking operations are allowed. So:
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in thebytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from0
to'47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.
– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand whereMath.floor(bignum / 0x100000000)
comes from?
– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
add a comment |
Most languages, including C and Javascript, have bit-shifting and bit-masking operations as part of their basic math operations. But beware Javascript: numbers are 64 bits, but only 32-bit masking operations are allowed. So:
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
Most languages, including C and Javascript, have bit-shifting and bit-masking operations as part of their basic math operations. But beware Javascript: numbers are 64 bits, but only 32-bit masking operations are allowed. So:
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
let bignum = Date.now();
let hi = Math.floor(bignum / 0x100000000),
lo = bignum & 0xFFFFFFFF,
bytes = [
(hi >> 24) & 0xFF,
(hi >> 16) & 0xFF,
(hi >> 8) & 0xFF,
hi & 0xFF,
(lo >> 24) & 0xFF,
(lo >> 16) & 0xFF,
(lo >> 8) & 0xFF,
lo & 0xFF
];
edited Nov 20 '18 at 22:44
JO3-W3B-D3V
1,135119
1,135119
answered Nov 20 '18 at 22:42
Lee Daniel Crocker
9,94611940
9,94611940
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in thebytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from0
to'47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.
– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand whereMath.floor(bignum / 0x100000000)
comes from?
– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
add a comment |
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in thebytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from0
to'47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.
– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand whereMath.floor(bignum / 0x100000000)
comes from?
– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in the
bytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from 0
to '47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.– Lance Pollard
Nov 20 '18 at 22:47
If the integer is a string and it's a "bigint" of arbitrary size I don't think this would work. I would like for it to handle that, not sure how you knew to have 8 items in the
bytes
array, and that probably wouldn't work for arbitrarily sized small/bigints ranging from 0
to '47171857151875817758571875815815782572758275672576575677'
or whatever arbitrary size.– Lance Pollard
Nov 20 '18 at 22:47
This may seem relatively straightforward to you, but I don't understand where
Math.floor(bignum / 0x100000000)
comes from?– zfrisch
Nov 20 '18 at 23:09
This may seem relatively straightforward to you, but I don't understand where
Math.floor(bignum / 0x100000000)
comes from?– zfrisch
Nov 20 '18 at 23:09
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
@zfrisch That's shifting the high 32 bits into the low 32 bits. Javascript >> 32 will just produce a 0.
– Lee Daniel Crocker
Nov 20 '18 at 23:48
add a comment |
I'm not sure if this is too brute-forcey for what you want, but you can take an arbitrary string and just do the long division into a unit8Array
.
Here's a function (borrowed liberally from here) that will convert back and forth from an arbitrarily long string:
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
No doubt, there are some efficiencies to be found (maybe you can avoid the interim arrays).
add a comment |
I'm not sure if this is too brute-forcey for what you want, but you can take an arbitrary string and just do the long division into a unit8Array
.
Here's a function (borrowed liberally from here) that will convert back and forth from an arbitrarily long string:
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
No doubt, there are some efficiencies to be found (maybe you can avoid the interim arrays).
add a comment |
I'm not sure if this is too brute-forcey for what you want, but you can take an arbitrary string and just do the long division into a unit8Array
.
Here's a function (borrowed liberally from here) that will convert back and forth from an arbitrarily long string:
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
No doubt, there are some efficiencies to be found (maybe you can avoid the interim arrays).
I'm not sure if this is too brute-forcey for what you want, but you can take an arbitrary string and just do the long division into a unit8Array
.
Here's a function (borrowed liberally from here) that will convert back and forth from an arbitrarily long string:
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
No doubt, there are some efficiencies to be found (maybe you can avoid the interim arrays).
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
function eightBit(str){
let dec = [...str], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function eightBit2String(arr){
var dec = [...arr], sum =
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
// sanity check
console.log("256 = ", eightBit('256'), "258 = ", eightBit('258'))
let n = '47171857151875817758571875815815782572758275672576575677'
let a = eightBit(n)
console.log("to convert:", n)
console.log("converted:", a.toString())
let s = eightBit2String(a)
console.log("converted back:", s)
edited Nov 20 '18 at 23:44
answered Nov 20 '18 at 23:38
Mark Meyer
36.3k32958
36.3k32958
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402560%2fhow-to-split-large-integer-into-an-array-of-8-bit-integers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not just get the components like the year, month, day, etc.?
– Barmar
Nov 20 '18 at 22:35
Why not using timestamp with time() and localtime() to get the time as well as its components?
– Tom Kuschel
Nov 20 '18 at 22:37
Sorry the date made it confusing, the date part is irrelevant.
– Lance Pollard
Nov 20 '18 at 22:37
What are you actually tryng to accomplish? You could easily split an n-bit integer into n/8 8 bit intgers, and reconstitute the original... but if you were actually looking for a more efficient way of encapsulating time, that would be pointless.
– bluejack
Nov 20 '18 at 22:38
Nothing to do with date/datetime, sorry about that. Updated the question. I want to convert any integer of arbitrary size to array of 8-bit integers.
– Lance Pollard
Nov 20 '18 at 22:39