LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] urg...

[ILUG] urg...

John Tobin tobinjt at netsoc.tcd.ie
Fri Feb 1 19:38:36 GMT 2002


On Fri, Feb 01, 2002 at 06:47:31PM +0000, kevin lyda wrote:
? ok, this is driving me nuts.  i have a string in perl.  let's call it $s.
? now i want to take $s 6 bits at a time and put each 6 bits (plus 32) into
? a byte in $e.  in other words each 3 bytes of $s become 4 bytes in $e.
? 
? anyone else play with bit shifting in perl?  it doesn't seem to be
? working so far.
? 
? this doesn't work and i feel like shooting it at this point:
? 
?     #!/usr/bin/perl
? 
?     $s = shift;
?     $s .= "\0" x ((length($s) % 3)? 3 - (length($s) % 3): 0);
?     $e = "";
?     while (length($s) > 0) {
?         $c = substr($s, 0, 3);
? 	$s = substr($s, 3);
? 	foreach $x (1...4){
? 	    $e .= chr((ord(substr($c, 0, 1)) & 0xfc) + 32);
? 	    $c <<= 6;
? 	}
?     }
?     $d = "";
?     print("original: $s(".length($s).")\nencoded: $e\ndecoded: $d\n");

This is completely untested but might be a place to start.

@bits = unpack ( "b*", $s );
# make sure we have a multiple of 8 bits ( or whatever number )
# may not be necessary if unpack doesn't strip leading 0s
$num_needed = 8 - ( ( scalar @bits ) % 8 );
unshift @bits, 0 x $num_needed;

$e = 0;
while ( @bits ) {
	$e << 2;
	for ( 1 .. 6 ) {
		$e = ( $e << 1 ) + shift @bits;
	}
	$e = $e + 32;
}

Hope this helps,

? 
? kevin

-- 
John
"That would preempt a bunch of problems involved in trying to reconstruct
exactly how the Perl 5 parser thinks, which nobody entirely understands."
			Larry Wall, 2001/04/20, perl6-language at perl.org
"Finger to spiritual emptiness underlying everything." -- How a Japanese C
manual referred to a "pointer to void".




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell