⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.java

📁 电信小灵通短信网关通讯协议SMGP的JAVA语言API开发包源代码
💻 JAVA
字号:
package com.ut.smgp.api.function;

import java.util.*;
import java.security.MessageDigest;

import com.ut.smgp.api.structure.*;

public class function {

  public function() {
  }
  //将字符串由StandardFrom编码格式转换为StandardTo编码的格式
        public  String codeConver(String Input, String  StandardFrom, String StandardTo)
       {
               byte[]  bytes;
               String conResult = new String();
               try
               {
                       bytes = Input.getBytes(StandardFrom);
                       conResult = new String (bytes, StandardTo);
               }

               catch(Exception e)
               {
                       //debugging begins here
                       System.out.println(e);
                       //debugging ends here
                       return("null");
               }
               return conResult;
       }

       //将字符串由StandardFrom编码格式转换无编码格式的字符串
       public  String clearConver(String Input, String  StandardFrom)
       {
               byte[]  bytes;
               String conResult = new String();
               try
               {
                       bytes = Input.getBytes(StandardFrom);
                       conResult = new String (bytes);
               }

               catch(Exception e)
               {
                       //debugging begins here
                       System.out.println(e);
                       //debugging ends here
                       return("null");
               }
               return conResult;
       }


   //将无编码格式的字符串转换为StandardFrom编码格式字符串
       public  String codeConver(String Input,String StandardTo)
       {
               byte[]  bytes;
               String conResult = new String();
               try
               {
                       bytes = Input.getBytes();
                       conResult = new String (bytes, StandardTo);
               }

               catch(Exception e)
               {
                       //debugging begins here
                       System.out.println(e);
                       //debugging ends here
                       return("null");
               }
               return conResult;
       }
       /**********************************************************
        *
        *
        * 对编码进行转换
        *
        */
       public String codeConver(String src,int msgFormat)
       {
         String initEncoding="GB2312";
         try{
          if (msgFormat == 8)
             return new String(src.getBytes(initEncoding), "UTF-16BE");
          else if(msgFormat==0)
             return new String(src.getBytes(initEncoding), "US-ASCII");
          else if(msgFormat==3)
             return new String(src.getBytes(initEncoding), "US-ASCII");
          else if(msgFormat==4)
             return new String(src.getBytes(initEncoding), "US-ASCII");
         }catch(IndexOutOfBoundsException e)
          {System.out.println("submitStruct String2Byte error:"+e);}
          catch(ArrayStoreException e)
          {System.out.println("submitStruct String2Byte error:"+e);}
          catch(Exception  e)
          {System.out.println("submitStruct String2Byte error:"+e);}
         return src;
       }
       /*******************************************************************
        *
        * 将字符串转换成某个编码类型的byte[]
        *

       public  byte[] getBytes(String src,String encoding)
        {
           byte[] temp =new byte[0];
          try{
             src=clearConver(src,"GB2312");
            temp = src.getBytes(encoding);

          }catch(IndexOutOfBoundsException e)
          {System.out.println("submitStruct String2Byte error:"+e);}
          catch(ArrayStoreException e)
          {System.out.println("submitStruct String2Byte error:"+e);}
          catch(Exception  e)
          {System.out.println("submitStruct String2Byte error:"+e);}
          return temp;
       }*/

  /**************************************************************************
   *
   * 将String转换成固定长度的byte数组
   */
  public  byte[] getBytes(String src,int len)
 {
    byte[] result=new byte[len];
   try{
      byte[] temp=src.getBytes();
     System.arraycopy(temp,0,result,0,temp.length);

   }catch(IndexOutOfBoundsException e)
   {System.out.println("submitStruct String2Byte error:"+e);}
   catch(ArrayStoreException e)
   {System.out.println("submitStruct String2Byte error:"+e);}
   catch(Exception  e)
   {System.out.println("submitStruct String2Byte error:"+e);}
   return result;
}
 public  byte[] getBytes(String[] src,int len)
 {
    byte[] result=new byte[len*src.length];
    for(int i=0;i<src.length;i++)
    {
       try{
         byte[] temp = src[i].getBytes("GB2312");
         //byte[] temp = src[i].getBytes();
         System.arraycopy(temp, 0, result, i * len, temp.length);
     } catch(IndexOutOfBoundsException e)
        {System.out.println("submitStruct String2Byte error:"+e);}
     catch(ArrayStoreException e)
        {System.out.println("submitStruct String2Byte error:"+e);}
     catch(Exception  e)
        {System.out.println("submitStruct String2Byte error:"+e);}
    }
   return result;
}

 /*************************************************************
  * 数据初始化
  *
  */

 public void memset(byte abyte0[], int i)
   {
       for(int j = 0; j < i; j++)
           abyte0[j] = 0;

   }
   /****************************************************
    *
    * 数组复制,与System.arraycopy()一样
    */
   public int strcpy(byte abyte0[], byte abyte1[], int i, int j)
  {
           int k;
           for(k = 0; k < j; k++)
               abyte0[k + i] = abyte1[k];

           abyte0[k + i] = 0;
           return k;
  }
 /****************************************************************************
  *
  * MD5加密
  *
  */

  public  byte[] getMD5(byte[] b,int len)
{
        MessageDigest digest;
        String algorithm = "MD5";
        try
        {
                digest = MessageDigest.getInstance(algorithm);
                    digest.update(b,0,len);
                    return digest.digest();
            }
          catch (Exception e)
          {
                  e.printStackTrace(System.err);
                  return null;
          }
}
  /*******************************************************************
   *
   * 将包头转换程byte[]型
   *
   */
  public byte[] head2Byte(SMGPHeadStruct head)
   {
       byte abyte0[] = new byte[12];
       System.arraycopy(int2Byte(head.packageSize), 0, abyte0, 0, 4);
       System.arraycopy(int2Byte(head.requestId), 0, abyte0, 4, 4);
       System.arraycopy(int2Byte(head.sequenceId), 0, abyte0, 8, 4);
       return abyte0;
   }

 /*************************************************************
 *
 *byte[]转换成整型
 *
 */
 public int byte2Int(byte b)
 {
   int j=(int)b;
   if (j >= 0)
     j =  b;
   else
     j =256 + b;
   return j;
 }
 public static int byte2Int ( byte b[] ) {
    int i1, i2, i3, i4;
    i1 = (int)(b[0]); if (i1<0) i1=(int)(256+i1);
    i2 = (int)(b[1]); if (i2<0) i2=(int)(256+i2);
    i3 = (int)(b[2]); if (i3<0) i3=(int)(256+i3);
    i4 = (int)(b[3]); if (i4<0) i4=(int)(256+i4);
    i4 = (int)(16777216*i4 + 65536*i3 + 256*i2 + i1);
    return i4;
  }
/*
  public int byte2Int(byte[] b)
      {
              int s = 0;
              for (int i = 0; i < 3; i++)
      {
              if (b[i] >= 0)
                      s = s + b[i];
              else
                      s = s + 256 + b[i];
              s = s * 256;
      }
      if (b[3] >= 0)
              s = s + b[3];
      else
              s = s + 256 + b[3];
      return s;
      }
*/
   public int byte2Int(byte[] b,int start,int len)
   {
     byte[] dat=new byte[len];
     System.arraycopy(b,start,dat,0,len);
     return byte2Int(dat);
   }
   /*************************************************************
    *
    *整型转换成byte[]
    *
    */
   public byte[] int2Byte(int i)
       {
           byte abyte0[] = new byte[4];
           abyte0[3] = (byte)i;
           i >>>= 8;
           abyte0[2] = (byte)i;
           i >>>= 8;
           abyte0[1] = (byte)i;
           i >>>= 8;
           abyte0[0] = (byte)i;
           return abyte0;
       }
  public byte[] int2Byte(int i,int len)
          {
              byte abyte0[] = new byte[len];
              for(int j=0;j<len;j++)
              {
                abyte0[len - j - 1] = (byte) i;
                i >>>= 8;
              }
              return abyte0;
          }
  /********************************************************************
   *
   * 获取当前时间,格式MMDDHHMMSS
   *
   */
  public String getCurrentTimeStamp()
  {
   // return "994573501";
    byte time[]=new byte[4];
       GregorianCalendar todaysdate = new GregorianCalendar();
         int month=todaysdate.get(Calendar.MONTH)+1;
         int day = todaysdate.get(Calendar.DAY_OF_MONTH);
         int hour = todaysdate.get(Calendar.HOUR_OF_DAY);
         int minute = todaysdate.get(Calendar.MINUTE);
         int second = todaysdate.get(Calendar.SECOND);
         String strMonth=Integer.toString(month);
         String strDay=Integer.toString(day);
         String strHour=Integer.toString(hour);
         String strMinute=Integer.toString(minute);
         String strSecond=Integer.toString(second);
         if(day<10) strDay="0"+strDay;
         if(month<10) strMonth="0"+strMonth;
         if(hour<10) strHour="0"+strHour;
         if(minute<10) strMinute="0"+strMinute;
         if(second<10) strSecond="0"+strSecond;
         return strMonth+strDay+strHour+strMinute+strSecond;
  }


  /*********************************************************************
   *
   * 字符串替换,
   * JDK1.3不提供,JDK1.4提供
   *
   */
  public String replace(String strSource,String strFrom,String strTo)
    {
      if(strSource==null) return "";
     String strDest = "";
     int intFromLen = strFrom.length();
     int intPos;

     while((intPos=strSource.indexOf(strFrom))!=-1)
     {
      strDest = strDest + strSource.substring(0,intPos);
      strDest = strDest + strTo;
     strSource = strSource.substring(intPos+intFromLen);
     }
     strDest = strDest + strSource;
     return strDest;
     //return other.ChangeToISO8859(strDest);
     }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -