Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-20-2002, 10:05 AM   PM User | #1
Algorithm
Regular Coder

 
Join Date: Jul 2002
Location: USA
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Algorithm is an unknown quantity at this point
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.

Code:
// 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){
	if(!this.writing){
		this.bits.reverse();
		this.writing = true;
	}
	bit = (bit)?1:0;
	this.bits.push(bit);
}

// 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;
	}
	if(noDrop){
		if(this.writing){
			return this.bits[0];
		}
		return this.bits[(this.bits.length-1)];
	}
	if(this.writing){
		this.bits.reverse();
		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;
	if(this.writing){
		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++){
			j--;
			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(){
	if(!this.writing){
		this.bits.reverse();
		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];
		i++;
		if((i%this.root)==0){
			txt = txt + this.charset.charAt(c);
			c=0;
		}
	}
	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){
	if(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){
		r++;
	}
	if(r>0){
		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++){
			this.bits.push(c%2);
			c -= (c%2);
			c /= 2;
		}
	}
}
Questions / comments / complaints are extremely welcome.

Last edited by Algorithm; 10-20-2002 at 10:27 AM..
Algorithm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 PM.


Advertisement
Log in to turn off these ads.