C#.net

Convert text to HexaDecimal PDU or hexa-decimal octets



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
1101000  
110010 1
11011 00
1101 100
110 1111
11 01000
1 100101
  1101100
1101100  
110111 1

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:

1 1101000
00 110010
100 11011
1111 1101
01000 110
100101 11
1101100 1
   
1 1101100
   

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”) + “”;
                }
            }

Share

Thanks for reading my blog. If you like what I write, why not subscribe to my feed?

If you are busy, I can send the latest post to your email. Just subscribe to my email updates.

 

Enter your email address:

Delivered by FeedBurner

Discussion

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

Post a comment