Ady Wicaksono Daily Activities

Archive for the ‘SMS’ Category

function convert8bitTo7bit with PHP

with one comment

As per GSM 03.40 we can send up to 140 octets (8-bit data), so if we send 7-bit data (septet) we can send up to 160 7-bit ASCII characters. Few days ago I need a function to convert a string to septet hexadecimal representation with PHP without any luck. I tried googling with keyword “convert 8bit to 7bit PHP”, “convert octet to septet PHP”, and similar keyword with no luck.

So I managed to create my own function that works fine so far (hopefully) (but I don’t care if my algo is not optimal nor bad:)) below:

function strToHex($string)
    for ($i=0; $i < strlen($string); $i++)
        $hex .= dechex(ord($string[$i]));
    return $hex;
function hexToStr($hex)
    for ($i=0; $i < strlen($hex)-1; $i+=2)
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    return $string;

function hexbin($hex){
       return $bin;

function binhex($bin){
   return strrev($hex);

function Convert8BitTo7Bit($string){
	// Convert String to Hex first
	// E.g *135# will be converted to 2A 31 33 35 23
	$string = strToHex($string);
	// print   "STR = $string\n";
	$total = "";
	for($i = 0; $i < strlen($string); ){
		// Get 1st character string, it's 2 character hex
		$X = $string[$i++].$string[$i++];
		// Convert it to binary
		$my8bit = hexbin($X);
		//print "(8bit) ==> $my8bit\n";
		// remove left side of octet, it shall be septet
		// e.g 2A in octet is 00101010 (8 bit), remove most left 0 --> 0101010 (7 bit)
		$my7bit = substr($my8bit,1,7);
		//print "(7bit) ==>  $my7bit\n";
        // Concatenate it
		$total = $my7bit.$total;
	// Padding the string
	if(strlen($total) % 8 != 0){
		$p1     = (intval((strlen($total) / 8)) + 1) *  8;
		$total  = str_pad($total,$p1,'0',STR_PAD_LEFT);
	$pad   = 7;
	// Conversion begin
	for($i = strlen($total) - 1; $i >= 0 ; $i--){
		$mypad[$pad--] = $total[$i];
		if($pad < 0 || $i <= 0){
			$pad  = 7;
			$tmp1 = array_reverse($mypad);
			$tmp2 = implode($tmp1);
			$res = binhex($tmp2);
			$result .= "$res";
	return $result;


To use that code, we simply call the function like this

print Convert8BitTo7Bit("*135#")."\n";

It will print “AAD8AC3602” which represent hexcode to be send on top of TP-UD GSM 03.40

Another wxample: we have 160 characters to send like below
“Test SMS content 160 characters will be displayed as 140 octets. Test SMS content 160 characters will be displayed as 140 octets.Test SMS content 160 characters”

With calling

print Convert8BitTo7Bit("Test SMS content 160 characters will be displayed as 140 octets. Test SMS content 160 characters will be displayed as 140 octets.Test SMS content 160 characters");

We get 140 octets/bytes hexadecimal code for this:


Anyway, there’s 1 thing missing here:
as per GSM 03.38

If the total number of characters to be sent equals (8n 1) where n=1,2,3 etc. then there are 7 spare bits at the end of the message. To avoid the situation where the receiving entity confuses 7 binary zero pad bits as the @ character, the carriage return or character (defined in clause 6.1.1) shall be used for padding in this situation, just as for Cell Broadcast.
If is intended to be the last character and the message (including the wanted ) ends on an octet boundary, then another must be added together with a padding bit 0. The receiving entity will perform the carriage return function twice, but this will not result in misoperation as the definition of in clause 6.1.1 is identical to the definition of .
The receiving entity shall remove the final character where the message ends on an octet boundary with as the last character.

So please fix this function and if the latest one is 0x00, replace it with 0X1A (Carriage Return) 😉


Written by adywicaksono

December 24, 2009 at 4:24 pm

Sending SMS to J2ME application

with 14 comments

The Wireless Messaging API (WMA) is an optional package for J2ME that
provides platform-independent access to wireless communication resources
like Short Message Service (SMS). If URI for HTTP is defined by http://
then URI for SMS resource is defined by sms://. Example resource identifier
for SMS is sms://:50000, means the port of SMS is 50000.

For example you have a J2ME application that listen to specific port, then
how to send SMS to the receiver would be this J2ME application? Simple approach
is to create another J2ME application that act as sms sender. But what is the
real PDU (protocol data unit) involved for sending it?

If you see GSM 03.40 specification, you will find that we need to set some
special UDH (user data header) inside the 140 octet of TP-UD. This UDH is like this
060504C3500000, the details is:
0x06 : TP-UDHL, means the user data header length -> length of “0504C3500000” = 6 octet
0x05 : Application port addressing scheme, 16 bit address, means we have port range up
to 2^16 = 65536, so port 50000 is in the range
0x04 : Length of application port addressing scheme -> length of “C3500000” = 4 octet
0xC350: Destination port, convert 0xC350 to decimal, you will have 50000
0x0000: Source port

For working example, please run this WMA java example taken from Sun JME toolkit
On the phone side we see that application is waiting for incoming SMS at port 50000


We’re now sending the SMS using AT+command, here’s the PDU:


To break down that PDU please refer to GSM 03.40 :), meanwhile I will focus on the 1st byte
that inform us that this PDU contain UDHI (user data header indicator) and the TP-UD of SMS
which is 1F060504C3500000496E6920636F6E746F6820666C61736820534D53206C686F

1F: TP-UDL (user data length)
060504C3500000 : this is the UDH (see above)
496E6920636F6E746F6820666C61736820534D53206C686F : this is the data

and here’s the screenshot at the mobile phone after receiving this SMS


Written by adywicaksono

February 10, 2008 at 6:52 am

Posted in j2me, SMS

How to send “Flash SMS”

with 8 comments

Flash SMS is a type of SMS which will be displayed directly on screen once received. This one is known working on Nokia mobile phone, I don’t know with others. As we know, normal SMS once received is saved either on SIM card or mobile phone memory and then user is noticed by a beep or similar SMS alarm. Flash SMS is different.

Flash SMS is done by define message class part of TP-DCS of SMS PDU to zero (0x00), what TP-DCS is? Please read GSM 03.40 :). Here’s an example of SMS PDU (in hexadecimal):


Let us breakdown:

  • 1st byte: 0x15 or in binary 00010101
    • TP-MTI (bit 0&1) = 01 means this PDU SMS type is SMS-SUBMIT
    • TP-RD (bit 2) = 1 means reject duplicate if there’s a same SMS with same TP-MR
    • TP-VPF (bit 4&3) = 10 means this SMS contain validity period info with relative format
    • TP-SRR (bit 5) = 0 means we don’t ask for delivery report
    • TP-UDHI (bit 6) = 0 means no user data header information (UDHI)
    • TP-RP (bit 7) = 0 means reply path unused
  • 2nd byte: TP-MR = 0x01, means message reference number is 0x01
  • 3rd byte: TP-DA length = 0x0A, this is length of destination address.I want to send to my own number which is 6591407482 -> see there’re 10 digits, to TP-DA should be 0x0A or 10 in decimal
  • 4th byte: TON/NPI info = 0x91 or in binary format 10010001
    • TON (bit 6,5,4) = 001 -> means International Number
    • NPI (bit 3,2,1,0) = 0001 -> means ISDN telephone number
  • 5th byte .. 9th byte: 5619044728 -> this is the destination number 6591407482 but written in BCD semioctet format
  • 10th byte: TP-PID = 0x00 -> means use default protocol identifier
  • 11th byte: TP-DCS = 0xF4 or in binary format 11110100
    • Bit 3,2 = 01, means TP-UD (user data) is encoded using 8 bit format
    • Bit 0,1 = 00, means class 0 {this will make this sms become flash sms}
  • 12th byte: TP-VP = 0xA7 means this SMS is valid only for 24 hours
  • 13th byte: TP-UDL = 0x18 means the user data/message part length is 24 character
  • the rest byte:
    • 496E6920636F6E746F6820666C61736820534D53206C686F

Now we try to send it using AT+Command, here’s an example session

flash sms at command

And the result on mobile phone is:

flash sms phone result

Now, save it before you lost it 🙂

Written by adywicaksono

February 8, 2008 at 2:56 am

Posted in SMS, Telco - GSM

Cool article on PDU SMS hacking

leave a comment »

Written by adywicaksono

November 1, 2007 at 10:01 am

Posted in Mobile, SMS