Category Archives: Uncategorized

Drill Battery Pack Rebuild

Hooray for cheap NiCd batteries from China. I just rebuilt a dead battery pack for my cordless drill an awful lot cheaper than buying a replacement one.




And to help it last longer, I also bought a smart charger. The original charger was really dumb and would overcharge the battery every time – which is why it blew up in the first place. The cells and the charger together still cost less than a replacement battery!




Drill Press

When you have to drill a couple of hundred holes, this cheap, handy drill press adapter from Jaycar turns your regular drill into a drill press.

Create random password in a shell script

Here’s a quick one-liner to make up a random password using only shell commands.
MYPASSWORD=`cat /dev/urandom | tr -dc a-z0-9 | head -c 10`
If you want to customize this a bit, there are two parts you can change, first is the filter which specified what range of characters are allowed in the password. a-z0-9 restricts the password to lower-case letters and digits only. You can add extra ranges and characters here, for example, to add a few punctuation characters, try a-z0-9$^&

The other customization is the length of the password which is the number at the end. In my case, the password will be 10 characters long, you can change it to anything you like.

Quick and dirty fix for corrupted mailbox

I run an old mail server using exim4 which stores its files in mbox format.

My inbox isn’t exactly small these days and I get a large volume of mail, mostly spam unfortunately. Sometimes exim4 has a glitch and corrupts the mailbox. The symptoms are always the same, the start of the mbox file is messed up with some random binary garbage. It always occurs at the very start of the file like this example:

(MʱÔ)wiÎ}1Þméõx¤¾GÊ6DFx£MVLZ¦B{TåQ}­ÃÈÍ ©F!¿ã¶Ùìããé!÷EúýD»Ü>W~sÃ.131.81])
        by adamserver with esmtp (Exim 4.69)
        (envelope-from )
        id 1O7eM3-000229-SH
        for; Fri, 30 Apr 2010 10:45:20 +1000
Received: by (Postfix, from userid 33)
        id 41A4CA7E0; Fri, 30 Apr 2010 00:36:38 +0000 (UTC)
Subject: Hello

Now trying to edit a 2GB mbox file using vi or nano is akin to torture so here’s my quick and dirty method which simply deletes the first message in the inbox using the commandline tool awk. I have used this technique 5 or 6 times in the last year with success.

NOTE: Make sure you shut down your SMTP server, POP3 and IMAP servers before you run this.

cd /var/spool/mail
mv adam adam-corrupt
tail -c +500 adam-corrupt | awk '/^From /,0' > adam
chown adam adam

How to cycle through the values of an Enum in Java

Here’s a quick little trick. I needed to advance my variable to the next value in an Enum. I found this can be done using the values and ordinal built-in members. This version wraps around to the beginning when you reach the end.

Here’s the code:

private enum Direction { NORTH, EAST, SOUTH, WEST;
public Direction getNext() {
return values()[(ordinal() + 1) % values().length];

Here’s an example of how to use it:

private Direction currentDirection = Direction.NORTH;

public void turn() {
currentDirection = currentDirection.getNext();

Steve the Weightlifter

SteveMy kids are heavily into Minecraft at the moment. Since I don’t have much to write about this week, let’s have a look at the thing that impressed me most about this game – the amazing weight-lifting skills of the game’s main character, Steve.

Steve is a pretty strong guy. Not only can he smash through trees and mountains with his bare fists, he’s always strolling around carrying big blocks of stone, whole trees and a small armoury of tools and weapons. Just for laughs, let’s work out the maximum weight he can carry.

Minecraft Inventory ScreenAll the stuff Steve is currently carrying is shown in his personal inventory.

Steve’s personal inventory has 36 slots and each slot can hold up to 64 items, this means he can carry a maximum of 2304 items.

So what is the heaviest item he can carry? Well the heaviest material in the game is gold so how about a cubic metre of gold? How much would that weigh?

Gold has a density of 19.3 grams per cubic centimetre, there are a million cubic centimetres in a cubic metre. So a cubic metre of gold would weigh 19.3 metric tonnes.

Just one of these gold blocks would defeat (and squash flat) the world’s best weightlifters. The current world weight lifting record stands at 263kg.

But Steve can carry 2304 items. If each gold block weighs 19.3 tonnes, that’s a total of 44,467 tonnes!

And even with all that weight in his pockets, it doesn’t even slow him down.

All that gold by the way would be worth about $2.6 trillion at today’s prices, you could probably buy Norway for that amount of cash, and Steve wouldn’t have to worry about anyone stealing it as nobody could possibly lift it.

An Olympic gold medal only contains 6 grams of gold, so you could make 7.4 billion of them by emptying Steve’s pockets. That’s an Olympic gold medal for everyone on the planet.

Hyperlinks for your twitter posts

Here’s a quick Javascript to take a string and automatically add hyperlinks to any twitter tags it sees. This could be useful if you were writing a web page for users to enter tweets. It will take a string such as “I gave @siliconsparrow a hug”, detect the at-tag and wrap it in the appropriate hyperlink.

Here’s a demo page where you can see it in action.

function addTwitterLinks(text) {
 return text.replace(/[\@\#]([a-zA-z0-9_]*)/g,
   function(m,m1) {
     var t = '<a href="';
     if(m.charAt(0) == '#')
       t += 'hashtag/';
     return t + encodeURI(m1) + '" target="_blank">' + m + '</a>';



New Toy

In the past I’ve written a couple of posts on how to get better magnification out of a digital camera. Well, you can get some pretty reasonably priced digital microscopes these days – and now I have one, the DigiTech QC3247.

It has two modes, a 20X mode which is fairly useful and a 400X mode which is ridiculous.

Here’s a capture from 20X mode. To give you an idea of scale, the brown chip capacitors in the picture are 0603 – ah, that is 1.6mm long and 0.8mm wide. Click on the picture to get the full-res 800×600 image.

Now here’s a capture of one of those same chip capacitors at 400X. It took me ages to position and focus it as the frame is less than 1mm from side to side. This mode is of limited use I think unless I build a mount for the microscope. It is impossible to hand-hold it at this magnification and the supplied stand is too flimsy to hold it in position without wobbling.

All in all, this will be a nice addition to my toolbox.