Monthly Archives: September 2009

Base Conversions in MySQL

Allow me to present dec2base() and base2dec(), a pair of MySQL stored procedures for converting to/from denary and other number bases. The examples below are for Base 53. Just add or remove characters from t_digits to change.

If you are curious about my choice of Base 53, it is because I have tried to minimise the use of similar-looking characters, to reduce transcription errors.

This code is based on an example in PL/SQL that I found on the JavaConfessions blog.

The reason for using varbinary() instead of varchar() for t_digits is that base2dec() will produce incorrect results due to case-insensitive matches on varchar().


drop procedure if exists dec2base;
delimiter //
create procedure dec2base(n int unsigned)
begin
declare t_modulo int unsigned;
declare t_int int unsigned;
declare t_val varchar(256);
declare t_char char(1);
declare t_digits varbinary(256);
declare t_base int unsigned;

set t_digits='23456789abcdefghjkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY';
set t_base=length(t_digits);

if (n=0) then
select 0;
else
set t_int=n;
set t_val='';

theLoop: loop
if (t_int=0) then
leave theLoop;
end if;
set t_modulo = t_int % t_base;
set t_char = substr(t_digits, t_modulo+1, 1);
set t_val = concat(t_char,t_val);
set t_int = floor(t_int/t_base);
end loop;

select t_val as base_value;

end if;
end;
//
delimiter ;

drop procedure if exists base2dec;
delimiter //
create procedure base2dec(c varbinary(256))
begin
declare t_iterator int unsigned;
declare t_length int unsigned;
declare t_char char(1);
declare t_int int unsigned;
declare t_retval int unsigned default 0;
declare t_mult int unsigned default 1;
declare t_convval varbinary(256);
declare t_digits varbinary(256);
declare t_base int unsigned;

set t_digits='23456789abcdefghjkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY';
set t_base=length(t_digits);

set t_convval=c;
set t_length=length(t_convval);
set t_iterator=t_length;

theLoop: loop
if (t_iterator=0) then
leave theLoop;
end if;
set t_char = substr(t_convval,t_iterator,1);
set t_int = instr(t_digits,t_char)-1;
set t_retval = t_retval + (t_int * t_mult);
set t_mult = t_mult * t_base;
set t_iterator = t_iterator -1;
end loop;

select t_retval as decimal_value;

end;
//
delimiter ;

Vodafone Australia Mobile Broadband – On Linux

I have just acquired a Huawei K3250 USB wireless modem with a Vodafone prepaid SIM.  There seem to be a lot of "recipes" floating around on how to get similar devices working on Linux, but several which supposedly work with the service from Vodafone Australia, in a word, don't.

My laptop is running Debian Lenny with a 2.6.26 kernel – recent kernel versions supporting the Huawei devices by default.

As I'm a bit old-fashioned and always used wvdial back in my dial-up days, this is what I have used this time around too.

This is the wvdial.conf that works for me:


[Dialer Defaults]
New PPPD = yes
Stupid Mode = 1
Init1 = AT
Init2 = ATZ
Init3 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init4 = AT+CGDCONT=1,"IP","vfinternet.au"
Modem Type = Analog Modem
ISDN = 0
Phone = *99***2#
Modem = /dev/ttyUSB0
username = vodafone
Password = vodafone
Dial Command = ATDT
Baud =460800

Issues that may be encountered include:

  • The user in question not being in the dialout group
  • /usr/sbin/pppd having the incorrect permissions (should be -rwsr-xr–)
  • Using one of the many example wvdial.conf's that gives a phone number of *99#. This was the last problem I encountered – I would get a connection and then the modem would get fed up and stop pppd. Only when I changed to *99***2# did things start working as they should.

My only complaint is that it ain't half slow after 1.5Mbps ADSL – but I've only got it to keep me working during ADSL outages and for the very rare occasions when I am not at home.

Why did I choose Vodafone? Well, my first choice would have been my excellent ISP, Internode, but their service is through a carrier that doesn't have coverage in this area. The only other carrier with service around here is Telstra/Bigpond – who demand an arm, both legs, and your first-born for the privilege of connecting to their network. They will also add insult to injury with what I would laughingly call their customer service.

And when it comes to service, Vodafone Australia may give itself a good pat on the back both for their sales staff who got my order through despite having a rural South Australian address (impossible online,) and for the chap who helped me out this morning getting the thing connected.