Friday, November 14, 2008

How to GBK(GB2312) and UTF-8 encoding conversion between in JavaScript

following js code show to how to convert text between Simplified Chinese GB 2312 text strings into UTF8 using JavaScript.
GB2312UTF8  = {
  Dig2Dec : function(s){
      var retV = 0;
      if(s.length == 4){
          for(var i = 0; i < 4; i ++){
              retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
          }
          return retV;
      }
      return -1;
  } ,

  Hex2Utf8 : function(s){
     var retS = "";
     var tempS = "";
     var ss = "";
     if(s.length == 16){
         tempS = "1110" + s.substring(0, 4);
         tempS += "10" +  s.substring(4, 10);
         tempS += "10" + s.substring(10,16);
         var sss = "0123456789ABCDEF";
         for(var i = 0; i < 3; i ++){
            retS += "%";
            ss = tempS.substring(i * 8, (eval(i)+1)*8);
            retS += sss.charAt(this.Dig2Dec(ss.substring(0,4)));
            retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
         }
         return retS;
     }
     return "";
  } ,

  Dec2Dig : function(n1){
      var s = "";
      var n2 = 0;
      for(var i = 0; i < 4; i++){
         n2 = Math.pow(2,3 - i);
         if(n1 >= n2){
            s += '1';
            n1 = n1 - n2;
          }
         else
          s += '0';
      }
      return s;
  },

  Str2Hex : function(s){
      var c = "";
      var n;
      var ss = "0123456789ABCDEF";
      var digS = "";
      for(var i = 0; i < s.length; i ++){
         c = s.charAt(i);
         n = ss.indexOf(c);
         digS += this.Dec2Dig(eval(n));
      }
      return digS;
  },

  GB2312ToUTF8 : function(s1){
    var s = escape(s1);
    var sa = s.split("%");
    var retV ="";
    if(sa[0] != ""){
      retV = sa[0];
    }
    for(var i = 1; i < sa.length; i ++){
      if(sa[i].substring(0,1) == "u"){
        //alert(this.Str2Hex(sa[i].substring(1,5)));
        retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
  if(sa[i].length){
    retV += sa[i].substring(5);
  }
      }
      else{
     retV += unescape("%" + sa[i]);
  if(sa[i].length){
    retV += sa[i].substring(5);
  }
   }
    }
    return retV;
  },

  UTF8ToGB2312 : function(str1){
        var substr = "";
        var a = "";
        var b = "";
        var c = "";
        var i = -1;
        i = str1.indexOf("%");
        if(i==-1){
          return str1;
        }
        while(i!= -1){
    if(i<3){
                substr = substr + str1.substr(0,i-1);
                str1 = str1.substr(i+1,str1.length-i);
                a = str1.substr(0,2);
                str1 = str1.substr(2,str1.length - 2);
                if(parseInt("0x" + a) & 0x80 == 0){
                  substr = substr + String.fromCharCode(parseInt("0x" + a));
                }
                else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
                        b = str1.substr(1,2);
                        str1 = str1.substr(3,str1.length - 3);
                        var widechar = (parseInt("0x" + a) & 0x1F) << 6;
                        widechar = widechar | (parseInt("0x" + b) & 0x3F);
                        substr = substr + String.fromCharCode(widechar);
                }
                else{
                        b = str1.substr(1,2);
                        str1 = str1.substr(3,str1.length - 3);
                        c = str1.substr(1,2);
                        str1 = str1.substr(3,str1.length - 3);
                        var widechar = (parseInt("0x" + a) & 0x0F) << 12;
                        widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
                        widechar = widechar | (parseInt("0x" + c) & 0x3F);
                        substr = substr + String.fromCharCode(widechar);
                }
     }
     else {
      substr = substr + str1.substring(0,i);
      str1= str1.substring(i);
     }
              i = str1.indexOf("%");
        }

        return substr+str1;
  }
};

test:
  GBK => UTF8:
  var utf8 = GB2312UTF8.GB2312ToUTF8("中文GB2312");

  UTF8 => GBK:
  GB2312UTF8.UTF8ToGB2312(utf8);
...
Read more
Monday, July 07, 2008

How to send messages using PHP

sendMsg is a class which allows you to send a message to a contact from a specified account. It logs in, changes status to online, creates a new IM session with the target user, sends the message and ends both connections.

The Messenger server imposes a limit on how many IM sessions can be created per minute, so the script is not suited for sending messages to large numbers of contacts within the same login session (see below).

The user sending the message must be on the allow list of the receiving user; or, the target user must have “All others” on their allow list (as in the Windows Live Messenger options dialog). To get the login ticket for authentication it uses the MSNPAuth class, which is included in the ZIP file.

Because of the way the protocol works, the sending user doesn’t appear to come online because it does not synchronize lists.

This class has not been extensively tested, if you find a bug or error, please post a comment here.
Using sendMsg
Standard sending precedure - logging in, creating an IM session and sending a message:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('recipient@hotmail.com');
$sendMsg->sendMessage('message', 'Times New Roman', 'FF0000');

The last two arguments for sendMessage() which specify font options are optional. The receiving client’s default font will be displayed if they are not specified.
createSession() should only be called a maximum of 8 times in any 60 second period, due to limits imposed by the Messenger service. The script will throw an error if you try calling createSession a 9th time.
Once createSession() has been called, sendMessage() can be used to send any number of messages to that user, for example:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('recipient@hotmail.com');
$sendMsg->sendMessage('this is the first message');
$sendMsg->sendMessage('this is the second message');
$sendMsg->sendMessage('this is the third message');

createSession() can be called for each user that requires a message to be sent to. To send two messages to two different users:
$sendMsg = new sendMsg();
$sendMsg->login('sender@hotmail.com','password');
$sendMsg->createSession('firstperson@hotmail.com');
$sendMsg->sendMessage('hello first person');
$sendMsg->createSession('secondperson@hotmail.com');
$sendMsg->sendMessage('hello second person);

There is also a function which simplifies the procedure of sending a message to a single user:
$sendMsg = new sendMsg();
$sendMsg->simpleSend('sender@hotmail.com','password', 'recipient@hotmail.com','message');

The result property can be used to see how it went, refer to the constant definitions at the top of sendMsg.php for textual meaning. For description of socket errors, you can check the property error.
You can give it a try at the sandbox. If you have troubles getting the script working with connections error, please refer to Troubleshooting phpListGrab (phpListGrab uses similar code).
From: http://www.fanatic.net.nz/2005/02/15/send-a-message-using-php/
...
Read more
Wednesday, June 25, 2008

How to send SMS in J2ME

The sample code on how to send SMS through J2ME.

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/** Sends an SMS message */
public class SMSender implements Runnable {
  private String smsReceiverPort;
  private String message;
  private String phoneNumber;

  public SMSender(String smsReceiverPort) {
    this.smsReceiverPort = smsReceiverPort;
  }

  public void run() {
    StringBuffer addr = new StringBuffer(20);
    addr.append("sms://+");
    if (phoneNumber.length() == 11)
      addr.append("86");//  china

    addr.append(phoneNumber);
    // String address = "sms://+8613641301055";
    String address = addr.toString();

    MessageConnection smsconn = null;
    try {
      // Open the message connection.
      smsconn = (MessageConnection) Connector.open(address);
      // Create the message.
      TextMessage txtmessage = (TextMessage) smsconn
          .newMessage(MessageConnection.TEXT_MESSAGE);
      txtmessage.setAddress(address);// !!
      txtmessage.setPayloadText(message);
      smsconn.send(txtmessage);
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (smsconn != null) {
      try {
        smsconn.close();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
  }

  public void send(String message, String phoneNumber) {
    this.message = message;
    this.phoneNumber = phoneNumber;
    Thread t = new Thread(this);
    t.start();
  }

}
...
Read more
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory