Hii friends…Here is a function in c#.net to convert a given text to octet..This might be useful if you are trying to convert text to PDU format for sending SMS from a mobile using c#.net through serial port……
ÂÂ
Procedure
Input text :”hellohello”
Step 1 : Convert each alphabet to ascii
Step 2 : convert ascii to 7 bit binary
Step 3 : Convert 7 bit binary to 8 bit
Step4:Convert 8 bit to hexadecimal
| h | e | l | l | o | h | e | l | l | o | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 104 | 101 | 108 | 108 | 111 | 104 | 101 | 108 | 108 | 111 | ||||||||||||||||||||
| 1101000 | 1100101 | 1101100 | 1101100 | 1101111 | 1101000 | 1100101 | 1101100 | 1101100 | 1101111 | ||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
The first septet (h) is turned into an octet by adding the rightmost bit of the second septet. This bit is inserted to the left which yields 1 + 1101000 = 11101000 (“E8″). The rightmost bit of the second character is then consumed, so the second character (septet) needs two bits (yellow) of the third character to make an 8bit octet. This process goes on and on yielding the following octets:
|
|
|
|
|
|
|
|
|
110111 |
||||||||||||||||||||
| E8 | 32 | 9B | FD | 46 | 97 | D9 |
|
EC | 37 |
The 9 octets from “hellohello” are E8 32 9B FD 46 97 D9 EC 37
ÂÂ
Function
 public void tohex(string message)
       {
           string msg = message;
           int[] a=new int[msg.Length*8];
           string[] splitstring = new string[msg.Length];
           string[] hexValue = new string[1000];
           int pos = 0;
           //Split the Message
           for (int i = 0; i < msg.Length; i++)
           {
               splitstring.SetValue(msg.Substring(i, 1), i);
           }
           //COnvert to Binary
           string[] bin = new string[splitstring.Length];
           for (int i = 0; i < splitstring.Length; i++)
           {
               a[i] = Convert.ToInt32(Convert.ToChar(splitstring[i].ToString()));
               bin[i]=Convert.ToString(a[i], 2);
           }
             //Convert to octect
           string[] oct = new string[100];
           int count = 1;
           for (int i = 0; i < bin.Length-1; i++)
           {
               string[] sub=new string[bin[i].Length];
               //Take i+1 th binary value
               for (int j = 0; j < bin[0].Length; j++)
                   {
                   sub.SetValue(bin[i+1].Substring(j, 1), j);
                   }
               string concat=string.Empty;
               //Make the string to concatanate
               for (int k = sub.Length – count; k <sub.Length ; k++)
                   {
                   concat = “”+concat + sub[k].ToString() + “”;
                   }
              ÂÂ
               string octal = string.Empty;
               octal = “” + octal + concat + “”;
               //COncatanation
               for (int l = 0; l < bin[i].Length-(count-1); l++)
                   {
                    sub.SetValue(bin[i].Substring(l, 1), l);
                    octal = “” + octal + sub[l].ToString() + “”;
                   }
                   hexValue[pos] = “” + octal + “”;
                   pos++;
                   count++;
                    if (count == 8   )
                     {
                   count = 1;
                   i = i + 1;
                  ÂÂ
                    }
                    //Last COlumn
                    octal = string.Empty;
               if(i==bin.Length-2)
               {
                   for (int l = 0; l < bin[i].Length – (count – 1); l++)
                   {
                       sub.SetValue(bin[i+1].Substring(l, 1), l);
                    ÂÂ
                       octal = “”+octal +sub[l].ToString() + “”;
                   }
                   hexValue[pos] = “” + octal + “”;
               }
               }
               //FromHex obj=new FromHex();
               //obj.CovertFromHex(hexValue,pos);
               string t=string.Empty;
               for (int i = 0; i <= pos; i++)
               {
                   int d = Convert.ToInt32(hexValue[i].ToString(), 2);
                   t = ” “+t+d.ToString(“X”) + “”;
               }
           }

Discussion
No comments for “Convert text to HexaDecimal PDU or hexa-decimal octets”