Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Thanked 0 Times in 0 Posts

    The BitString class (compression, encryption, etc.)

    The BitString class converts back and forth between strings and bit arrays, based on a predefined charset. The bit array can be constructed or accessed either one bit at a time, or altogether.

    The code's primary purpose is for quick and easy data compression, but by changing the charset, one could easily use this code for simple encryption as well.

    Please note that if you intend to use this for huge (i.e. larger than 1kb) chunks of data, you should probably break it up into smaller pieces before feeding it through.

    edit: I can't get the charset declaration to display correctly, but for the record, it should NOT have a space at the beginning, and it SHOULD be all on one line. rrgh.

    // BitString encoder script (C)2002 Null Interactive.
    // Free use of this script is allowed as long as
    // this message remains intact.
    //            http://www.nullinteractive.com/
    function BitString(){
    	// These values should be treated as private.
    	// DO NOT employ these values in outside code.
    	// Use the methods provided.
    	this.charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
    	this.root = 6;
    	this.bits = new Array();
    	this.writing = false;
    	return this;
    // BitString.addBit(bit as anything)
    // Adds a bit value to the array.
    // The value is determined by whether bit evaluates to true.
    BitString.prototype.addBit = function(bit){
    		this.writing = true;
    	bit = (bit)?1:0;
    // BitString.getBit([noDrop as boolean = false])
    // Returns the first value added to the array,
    //   or if the array is empty, returns 0.
    // If noDrop evaluates to true, the value will be kept;
    //   otherwise it is discarded.
    // Please note that getBit always returns the first value,
    //   so if noDrop is specified, the same value will be
    //   returned the next time.
    BitString.prototype.getBit = function(noDrop){
    	if(this.bits.length == 0){
    		return 0;
    			return this.bits[0];
    		return this.bits[(this.bits.length-1)];
    		this.writing = false;
    	return this.bits.pop();
    // BitString.getBits()
    // Returns the entire bit array as an array of integers (0 or 1).
    // The array is read from left to right when using getBit.
    //   (The element at index 0 will be the first one returned.)
    BitString.prototype.getBits = function(){
    	var bary = new Array();
    	var i;
    		for(i=0; i<this.bits.length; i++){
    			bary[i] = this.bits[i];
    	} else {
    		var j = this.bits.length;
    		for(i=0; i<this.bits.length; i++){
    			bary[i] = this.bits[j];
    	return bary;
    // BitString.getText()
    // Returns a string encoded from the bit array.
    // If this string is fed into setText() with the
    // same charset, the same bit array will result.
    BitString.prototype.getText = function(){
    		this.writing = true;
    	var txt = "";
    	var c=0;
    	var i=0;
    	while((i<this.bits.length) || ((i%this.root)>0)){
    		c *= 2;
    		c += (i>=this.bits.length)?0:this.bits[i];
    			txt = txt + this.charset.charAt(c);
    	return txt;
    // BitString.length()
    // Returns the length of the bit array.
    BitString.prototype.length = function(){
    	return this.bits.length;
    // BitString.setBits([bitArray as Array = nothing])
    // Assigns a new array to the object.
    // If nothing is specified, the array will be emptied.
    BitString.prototype.setBits = function(bitArray){
    		var i;
    		for(i=0; i<bitArray.length; i++){
    			this.bits[i] = (bitArray[i])?1:0;
    		this.bits.length = bitArray.length;
    	} else {
    		this.bits.length = 0;
    	this.writing = true;
    // BitString.setCharset(txt as string)
    // Assigns a new charset to the object.
    // In order for the object to function properly, the
    //   charset must not contain any duplicate characters.
    BitString.prototype.setCharset = function(txt){
    	var p;
    	var r = 0;
    	for(p=2; p<=txt.length; p*=2){
    		this.root = r;
    		this.charset = txt;
    // BitString.setText(txt as string)
    // Fills the bit array based on the contents of
    //   txt and the current charset.
    BitString.prototype.setText = function(txt){
    	var i; var j; var c;
    	this.bits.length = 0;
    	this.writing = false;
    	for(i=txt.length-1; i>=0; i--){
    		c = this.charset.indexOf(txt.charAt(i));
    		if(c<0) c=0;
    		for(j=0; j<this.root; j++){
    			c -= (c%2);
    			c /= 2;
    Questions / comments / complaints are extremely welcome.
    Last edited by Algorithm; 10-20-2002 at 11:27 AM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts