Monday 30 January 2012

javascript: base64 encoding

It's quite easy to implement. See the code snippets below


<script>
    base64Keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
    base64Encode = function(input) {
      var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i, output;
      output = "";
      i = 0;
      input = utf8Encode(input);
      while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
          enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
          enc4 = 64;
        }
        output = "" + output + (base64Keys.charAt(enc1)) + (base64Keys.charAt(enc2)) + (enc3 < 64 ? base64Keys.charAt(enc3) : '') + (enc4 < 64 ? base64Keys.charAt(enc4) : '');
      }
      return output;
    };

    utf8Encode = function(string) {
      var c, s, utftext, _i, _len, _ref;
      string = string.replace(/\r\n/g, "\n");
      utftext = "";
      _ref = string.split('');
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        s = _ref[_i];
        c = s.charCodeAt(0);
        if (c < 128) {
          utftext += String.fromCharCode(c);
        } else if (c > 127 && c < 2048) {
          utftext += String.fromCharCode((c >> 6) | 192);
          utftext += String.fromCharCode((c & 63) | 128);
        } else {
          utftext += String.fromCharCode((c >> 12) | 224);
          utftext += String.fromCharCode(((c >> 6) & 63) | 128);
          utftext += String.fromCharCode((c & 63) | 128);
        }
      }
      return utftext;
    };
</script>
 

Friday 27 January 2012

Command-line to launch Android emulator.

Assuming Android SDK has been successfully installed and AVD of your target android version was created. For Window 7 user, the install path is likely at "c:\Program Files\Android\android-sdk\tools\". If you have problems on above step, please refer to Android developer site or email me. The command line to launch your emulator, for example IceCream.

c:\>"C:\Program Files\Android\android-sdk\tools\emulator.exe" -avd IceCream

Javascript to request GPS location from web page

There is no direct method natively built in Javascript stack. Don't give up though. I am giving your few lines of code to provide the result you look for. The code runs well on IE9, Chrome, FireFox, Safari on both PC and mobile device platforms.

 
function requestGeoLocation() {
 if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(loc){
 // do nothing
 //document.getElementById('myloc').innerHTML = 'Location: ' + 
 //    loc.coords.latitude + ', ' +
 //    loc.coords.longitude;
    
        }, function() { alert('request denied or something went wrong.');});
} else { 
 alert('GPS is supported.'); 
}
 

Tuesday 24 January 2012

bada mobile platform for C++ and Flash developersd

bada is Korean name meaning "ocean" or "sea". As you could guess, it's provided by another mobile device manufacture outside the North American region, called Samsung. Many mobile/tablet developers probably already knew much as objective-c and android. If you were hard core C++ fans like me, you probably want to take look at bada which provides you a ever familiar syntax and powerful OOD support. bada's home site is here.

bada IDE is eclipse CDT-base (C/C++ Development Tools). So you don't have to learn everything from the beginning if knows eclipse.







A new project wizard dialog pops up from "File->New->bada C++/Flash Application Project"

Thursday 12 January 2012

The database principal owns a schema in the database, and cannot be dropped

Encounter this while restoring a SQL Server database backup from one server to another, and trying to delete a user comes from the backup.

The workaround is to list all the schema that user owns by following script:
SELECT name FROM  sys.schemas WHERE principal_id = USER_ID('usertodelete');

then remove or transfer those schema owned by that user to another user, such as dbo by this script:
alter authorization on schema::schema_name to dbo;