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

📄 smstools.java

📁 自己实现的基于串口通讯发送短消息的JAVA代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }  // if

    switch (b) {
      case 0x00 : c = '@'; break;
      case 0x02 : c = '$'; break;
      case 0x0A : c = '\n'; break;
      case 0x0D : c = '\r'; break;
      case 0x11 : c = '_'; break;
      case 0x1E : c = '?'; break;
      case 0x20 : c = ' '; break;
      case 0x21 : c = '!'; break;
      case 0x22 : c = '\"'; break;
      case 0x23 : c = '#'; break;
      case 0x25 : c = '%'; break;
      case 0x26 : c = '&'; break;
      case 0x27 : c = '\''; break;
      case 0x28 : c = '('; break;
      case 0x29 : c = ')'; break;
      case 0x2A : c = '*'; break;
      case 0x2B : c = '+'; break;
      case 0x2C : c = ','; break;
      case 0x2D : c = '-'; break;
      case 0x2E : c = '.'; break;
      case 0x2F : c = '/'; break;
      case 0x3A : c = ':'; break;
      case 0x3B : c = ';'; break;
      case 0x3C : c = '<'; break;
      case 0x3D : c = '='; break;
      case 0x3E : c = '>'; break;
      case 0x3F : c = '?'; break;
      case 0x5B : c = '?'; break;
      case 0x5C : c = '?'; break;
      case 0x5E : c = '?'; break;
      case 0x5F : c = '?'; break;
      case 0x7B : c = '?'; break;
      case 0x7C : c = '?'; break;
      case 0x7E : c = '?'; break;
      default:    c = ' '; break;
    }  // switch
    return c;
  }  // convertGSM2Unicode

  /**
   * Compress a readable text message into the GSM standard alphabet
   * (1 character -> 7 bit data)
   * @param data text string in Unicode
   * @return text string in GSM standard alphabet
   */
  public static byte[] compress(byte[] data) {
    int l;
    int n;  // length of compressed data
    byte[] comp;

    // calculate length of message
    l = data.length;
    n = (l * 7) / 8;
    if ((l * 7) % 8 != 0) {
      n++;
    }  // if

    comp = new byte[n];
    int j = 0;   // index in data
    int s = 0;   // shift from next data byte
    for (int i = 0; i < n; i++) {
      comp[i] = (byte)((data[j] & 0x7F) >>> s);
      s++;
      if (j + 1 < l) {
        comp[i] += (byte)((data[j + 1] << (8 - s)) & 0xFF);
      }  // if
      if (s < 7) {
        j++;
      }  // if
      else  {
        s = 0;
        j += 2;
      }  // else
    } // for
    return comp;
  }  // compress

  /**
   * Extracts from a given SMS the Text
   * @param data SMS string
   * @return text date, time and SMS text string in Unicode
   * specification of the coding: TS 23.040
   * test pattern data: 0791947101670000040C9194617011078800004010023112914016D737DB7C0EBBCF2069D8BD66B340CDBA3B3D4603
     ADDRESS OF DELIVERING SMSC
  NUMBER IS :   +491710760000
  TYPE OF NR. : (0x10) International
  NPI :	      (0x01) ISDN/Telephone (E.164/163)
     MESSAGE HEADER FLAGS (0x04)
  MESSAGE TYPE :	SMS DELIVER
     ORIGINATING ADDRESS
  NUMBER IS :   +491607117088
  TYPE OF NR. : (0x10) International
  NPI :         (0x01) ISDN/Telephone (E.164/163)
     PROTOCOL IDENTIFIER (0x00)
     DATA CODING SCHEME (0x00)
  COMPRESSION :   OFF
  MESSAGE CLASS : NONE
  ALPHABET USED : 7 bit default
     SMSC TIMESTAMP : 20.01.04 13:21:19 GMT+1,00
     USER DATA PART OF SM
  USER DATA LENGTH :  22 septets
  USER DATA (TEXT) :  Wolfgang Rankl, Munich
  */
  public static String getSMSText(String data) {
    int i, x, n;
    String s, date="", time="", timezone="", orgnumber="";

    // System.out.println(data);          // output for debuging
    s = data.substring(0, 2);          // get length [byte] of delivering SMSC number
    //System.out.println("s:"+s);
    x = Integer.parseInt(s, 16); // calculate length [byte] of delivering SMSC number
    s = data.substring(0, 2+x*2);      // get raw delivering SMSC number, this line is optional for debugging reasons

    i = 2 + x * 2;                     // set index to message header flags
    s = data.substring(i, i+2);        // get message header flags

    i = i + 2;                         // set index to length [digits] of originating adress
    s = data.substring(i, i+2);        // get length [digits] of originating adress
    x = Integer.parseInt(s, 16);       // calculate length [digits] of originating adress
    s = data.substring(i+3, i+4);      // get type of number
    if (s.compareTo("1") == 0) {       // it is a national (0x81) or  international (0x91) number
      s = data.substring(i, i+x+4);    // get raw originating adress, this line is optional for debugging reasons
      orgnumber = decodeAddressField(s);
    } // if
    else {                             // it is a unknown type of number
      s = data.substring(i, i+2) + data.substring(i+4, i+4+x);    // get raw originating adress, this line is optional for debugging reasons
      orgnumber = decodeAddressField(s);
    } // else

    i = i + 6 + x;                // set index to data coding scheme
    s = data.substring(i, i+2);   // get raw data coding scheme, this line is optional for debugging reasons

    //----- get data, time and time zone
    i = i + 2;                     // set index to date and time (= TP-Service-Centre-Time-Stamp (TP-SCTS))
    s = data.substring(i, i+14);   // get raw date and time, this line is optional for debugging reasons

    date = s.substring(1, 2) + s.substring(0, 1) ;                 // get year
    date = s.substring(3, 4) + s.substring(2, 3) + "." + date;     // get month
    date = s.substring(5, 6) + s.substring(4, 5) + "." + date;     // get day

    time = s.substring(11, 12) + s.substring(10, 11);              // get hour
    time = s.substring(9,  10) + s.substring(8,   9) + ":" + time; // get minute
    time = s.substring(7,   8) + s.substring(6,   7) + ":" + time; // get second

    timezone = s.substring(13, 14) + s.substring(12, 13);          // get time zone

    i = i + 14;                                // set index to length of user data (=SMS)
    s = data.substring(i, i+2);                // get length of user data (=SMS)
    x = Integer.parseInt(s, 16);               // calculate length [characters] of user data (=SMS)
    data = data.substring(i+2, data.length()); // delete the transport information at the beginning of the PDU

    //----- copy SMS from a string into a byte array to prepare convertion to Unicode
    byte sms[] = new byte[data.length()/2];
    for (n = 0; n < data.length()/2; n++) {
      s = data.substring(n*2, n*2+2);
      sms[n] = (byte)(0x000000FF & Integer.parseInt(s, 16));
    }  // for
    data = expand(sms);

    //----- put all informations together
    data = orgnumber + " " + date + " " + time + " +" + timezone + " " + SMS.SMSTEXTSIGN + data;
    return data;
  }  // getSMSText

  /**
   * Expands a compressed GSM message in a readable text message
   * (7 bit data -> 1 character)
   * @param indata text string in GSM standard alphabet
   * @return text string in Unicode
   */
  public static String expand(byte[] indata) {
    int x, n, y, Bytebefore, Bitshift;
    String msg = new String("");
    byte data[] = new byte[indata.length+1];

    for (n = 1; n < data.length; n++) {
      data[n] = indata[n-1];
    } // for

    Bytebefore = 0;
    for (n = 1; n < data.length; n++) {
      x = (int) (0x000000FF & data[n]);   // get a byte from the SMS
      Bitshift = (n-1) % 7;               // calculate number of neccssary bit shifts
      y = x;
      y = y << Bitshift;                  // shift to get a conversion 7 bit compact GSM -> Unicode
      y = y | Bytebefore;                 // add bits from the byte before this byte
      y = y & 0x0000007F;                 // delete all bits except bit 7 ... 1 of the byte
      msg = msg + convertGSM2Unicode(y);  // conversion: 7 bit GSM character -> Unicode
      if (Bitshift == 6) {
        Bitshift = 1;
        y = x;
        y = y >>> Bitshift;                 // shift to get a conversion 7 bit compact GSM -> Unicode
        y = y & 0x0000007F;                 // delete all bits except bit 7 ... 1 of the byte
        msg = msg + convertGSM2Unicode(y);  // conversion: 7 bit GSM character -> Unicode
        Bytebefore = 0;
      }  // if
      else {
        Bytebefore = x;
        Bitshift = 7 - Bitshift;
        Bytebefore = Bytebefore >>> Bitshift;  // shift to get a conversion 7 bit compact GSM -> Unicode
        Bytebefore = Bytebefore & 0x000000FF;  // mask for one byte
      }  // else
    }  // for
    return msg;
  }  // expand


  /**
   * Convert data into a hex string
   * @param data to convert
   * @return in hex string converted data
   */
  public static char[] toHexString(byte[] data) {
    int l = data.length;
    char[] hex = new char[2 * l];

    int j = 0; // index in hex
    for (int i = 0; i < data.length; i++) {
      switch (data[i] & 0xF0) {
        case 0x00: hex[j] = '0'; break;
        case 0x10: hex[j] = '1'; break;
        case 0x20: hex[j] = '2'; break;
        case 0x30: hex[j] = '3'; break;
        case 0x40: hex[j] = '4'; break;
        case 0x50: hex[j] = '5'; break;
        case 0x60: hex[j] = '6'; break;
        case 0x70: hex[j] = '7'; break;
        case 0x80: hex[j] = '8'; break;
        case 0x90: hex[j] = '9'; break;
        case 0xA0: hex[j] = 'A'; break;
        case 0xB0: hex[j] = 'B'; break;
        case 0xC0: hex[j] = 'C'; break;
        case 0xD0: hex[j] = 'D'; break;
        case 0xE0: hex[j] = 'E'; break;
        case 0xF0: hex[j] = 'F'; break;
      } // switch
      j++;
      switch (data[i] & 0x0F) {
        case 0x00: hex[j] = '0'; break;
        case 0x01: hex[j] = '1'; break;
        case 0x02: hex[j] = '2'; break;
        case 0x03: hex[j] = '3'; break;
        case 0x04: hex[j] = '4'; break;
        case 0x05: hex[j] = '5'; break;
        case 0x06: hex[j] = '6'; break;
        case 0x07: hex[j] = '7'; break;
        case 0x08: hex[j] = '8'; break;
        case 0x09: hex[j] = '9'; break;
        case 0x0A: hex[j] = 'A'; break;
        case 0x0B: hex[j] = 'B'; break;
        case 0x0C: hex[j] = 'C'; break;
        case 0x0D: hex[j] = 'D'; break;
        case 0x0E: hex[j] = 'E'; break;
        case 0x0F: hex[j] = 'F'; break;
      } // switch
      j++;
    }  // for
    return hex;
  } // toHexString

  public static void main(String[] args){
	  
	  /****************METHOD 1*******************
	  String str ="Just for a test!";
	  char[] chrs = str.toCharArray();
	  String temp = SMSTools.convertCharArray2String(chrs);
	  
	  System.out.println("temp: "+temp);*/
	 /* byte[] bytes = null;
	  bytes = SMSTools.getPDUPart("15088610090", true, "你好");
	  char[] chrs = SMSTools.toHexString(bytes);
	  for(int i=0;i<chrs.length;i++){
		  
		  System.out.print(chrs[i]);
	  }*/
	  String temp = SMSTools.getSMSText("0891683108501705F031000D91685180680190F1000800040072597D");
	  System.out.println("test:"+temp);
  }
} // SMSTools


⌨️ 快捷键说明

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