iNerd
08-22-2003, 02:48 PM
If I have a large number like (new Date().getTime()), how do I seperate it into an array of byte-sized numbers, each number the next eight bits of the big number?
|
Click to See Complete Forum and Search --> : How do I split a large number into 8 bit numbers? iNerd 08-22-2003, 02:48 PM If I have a large number like (new Date().getTime()), how do I seperate it into an array of byte-sized numbers, each number the next eight bits of the big number? David Harrison 08-22-2003, 03:09 PM This should work but I haven't tested it: var big_number=(new Date().getTime())+""; var num_array=new Array(); for(var n=0;n<big_number.length;n++){ num_array[n]=big_number.substring(n,(n+1)); } iNerd 08-22-2003, 03:39 PM narg! That splits the number into base-ten digits. I need to split it into every 8 bits Example, with binary: bigNumber= 1111011100101011010110011111001000111111 every8bits= [11110111,00101011,01011001,11110010,00111111] It would be something with left and right bit-shift operators (<< and >>): 0x996A<<24>>24==0x6A 0x997F<<24>>24==0x7F 0x9980<<24>>24==-0x80 0x99FF<<24>>24==-1 :confused: David Harrison 08-22-2003, 03:53 PM var big_number=(new Date().getTime())+""; var num_array=new Array(); var n=0; var o=0; while(n<big_number.length;){ n+=8; o++; num_array[o]=big_number.substring(n,(n+8)); } var bin_big_number=num_array.join(",") If you can convert big_number into binary, then this script will assign a variable (bin_big_number) a binary number in the form: 11110111,00101011,01011001,11110010,00111111 Charles 08-22-2003, 05:33 PM <script type="text/javascript"> <!-- String.prototype.reverse = function () {return this.split('').reverse().join('')} Number.prototype.toBytes = function () {return this.toString(2).reverse().match(/\d{8}/g).join(',').reverse()} alert (new Date().getTime().toBytes()) // --> </script> iNerd 08-23-2003, 01:34 AM Well, I was expecting something like using bit-shift, but I found out that JavaScript bit-shift operands don't work with more than 31 bits (at least on my platform) :mad: .... Using mod (%) to take the last 31 bits, I can demonstrate what I was trying to work with: z=new Date.getTime()%0x80000000;n=[(z>>>24),(z>>>16<<24>>>24),(z>>>8<<24>>>24),(z<<24>>>24)];alert(n.toString()); Something on that grounds should work but I guess the JavaScript bit-shift operands expect a signed 32 bit integer :rolleyes: .... Oh whell, thanks anyway, I'll leave it open for a little while incase anyone else has any suggestions. I was trying to make it finite, in as few steps as possible, but if I want to use more than 31 bits than I'll have to use your method. Good job anyway. Thanks again, iNerd. Jeff Mott 08-23-2003, 12:07 PM I was expecting something like using bit-shiftWhat you describe would have looked something like thisvar n = new Date().getTime(); var a = new Array(); while (n) { a.push(n & 255); n >>= 8; }...but as the discovered on your own, the number you're working with is too big. Charles' method is really the best way to go. iNerd 08-23-2003, 02:08 PM Ya, that's pretty good :). Here's what I came up with while I was waiting for a reply: t=new Date().getTime(); z=t%0x80000000; y=(t-z)/0x80000000; n=[y>>>1, (y<<31>>>31)|(z>>>24), z>>>16<<24>>>24, z>>>8<<24>>>24, z<<24>>>24]; The point is so that I can display large numbers (including the clock, pseudo-random, and user-entered) as string, for an interactive lesson on how different concepts of computing work: t=new Date().getTime(); z=t%0x80000000; y=(t-z)/0x80000000; n=[y>>>1, (y<<31>>>31)|(z>>>24), z>>>16<<24>>>24, z>>>8<<24>>>24, z<<24>>>24]; s=String.fromCharCode(n[0], n[1], n[2], n[3], n[4]); The tutorial will also explain how things like pseudo-random number generators work, and display things at each level (binary, hex, string; machine language, assembly, high-level). It will include a converter to display equivilant string, alphanumeric (because it looks cool), hex, decimal, octal, and binary. What I came up with looks like this: i=new Date().getTime();//getNumber(); z=i%0x80000000; y=(i-z)/0x80000000; n=[y>>>1, (y<<31>>>31)|(z>>>24), z>>>16<<24>>>24, z>>>8<<24>>>24, z<<24>>>24]; s=String.fromCharCode(n[0], n[1], n[2], n[3], n[4]); a=i.toString(36); h=i.toString(16).toUpperCase(); d=i.toString(); o=i.toString(8); b=i.toString(2); while(b.length%8) b="0"+b; except i (for input) actually = the result of the function getNumber() which gets a number from the user. Your methods are good, though, and I'll probably improve mine a bit using your advice (just to pro-up the code) ;). Thanks again, iNerd Charles 08-23-2003, 04:13 PM Please forgive us if our humble efforts weren't up to your professional standards. iNerd 08-23-2003, 08:15 PM :confused: I said that I would improve my code with your examples, that I would pro-up my own code by using yours. :rolleyes: Sorry, I guess I wasn't clear enough. Bye, iNerd Charles 08-24-2003, 06:21 AM Forgive me. It seems that I did misread your post. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |