2013-03-06 120 views
0

我有我想要解析的以下十六進制代碼。對於好奇的人來說,這是一個彩信包。十六進制代碼的Java模式

e306246170706c69636174696f6e2f766e642e7761702e6d6d732d6d65737361676500af84b4818c82986e323333426178773042737549684d008d918918802b33333631313131313131312f545950453d504c4d4e0086818a808e02271088058103093a8083687474703a2f2f772e732f6158585842737549684d00 

詳細的版本是在這裏:

e3 // Wsp transactionId 
06 // Push type 
24 // Length of the content type field and headers 
6170706c69636174696f6e2f766e642e7761702e6d6d732d6d657373616765 // application/vnd.wap.mms-message 
00 // Null terminated 
af84 // X-Wap-Application-ID 
b481 // ? 
8c82 // X-MMS-Message-Type:m-notification-ind 
986e323333426178773042737549684d00 // Transaction id random with seed 
8d // MMS version 
91 // 1.1 
89 // From 
18 // Length of from 
80 // Address present 
2b3333363131313131313131 // Sender phone number 
2f545950453d504c4d4e // /TYPE=PLMN 
00 // Null terminated 
8681 
8a // Message class 
80 // Personnal (81=Ad, 82=Informational, 83=Auto) 
8e // Message size 
02 // Size will be coded on 2 bytes 
2710 // 10000 bytes 
88 // X-Mms-Expiry 
05 // Field size 
81 // Relative date format 
03 // Time will be coded on 3 bytes 
093a80 // Seconds 
83 // X-MMS-Content-Location 
687474703a2f2f772e732f6158585842737549684d // Url to the server 
00 // Null terminated 

我需要得到的電話號碼,2b3333363131313131313131在我的十六進制代碼和網址,這是687474703a2f2f772e732f6158585842737549684d服務器。 我正在Java for Android上工作。我幾乎肯定我需要使用模式,但我不明白它是如何工作的。

我已經做了以下的一段代碼(這顯然不工作):

  • ******中國模式:

    String number = hexCode.replace("^89([a-f0-9])00$", "$1"); 
    number = number.substring(6, number.length() - 22); 
    // Number should be equal to 2b3333363131313131313131 
    
  • 網址模式

    String url = hexCode.replace("^83([a-f0-9])00$", "$1"); 
    url = url.substring(2, url.length() - 2); 
    // Url should be equal to 687474703a2f2f772e732f6158585842737549684d 
    

你能幫我做嗎這些模式的工作?

編輯: 我能夠做到沒有模式的工作,但我不認爲這是最好的解決方案。

// Try to find the pattern "http" 
if (pduS.indexOf("68747470") > -1) 
{ 
    String url = pduS.substring(pduS.indexOf("68747470"), pduS.length() - 2); 
    Log.e(">>>>>>", "Url: " + convertHexToString(url)); 
} 

// Try to find the pattern "/TYPE=PLMN" 
if (pduS.indexOf("2b") > -1 && pduS.indexOf("2f545950453d504c4d4e") > -1) 
{ 
    String shortPdu = pduS.substring(0, pduS.indexOf("2f545950453d504c4d4e")); 
    String number = pduS.substring(shortPdu.lastIndexOf("2b"), pduS.indexOf("2f545950453d504c4d4e")); 
    Log.e(">>>>>>", "Number: " + number); 
    Log.e(">>>>>>", "Number: " + convertHexToString(number)); 
} 
+0

「PhoneNumber」和「URL」的位置是否總是固定的? – Apurv 2013-03-06 09:31:48

+1

試試這個答案。 http://stackoverflow.com/questions/7334244/android-mms-parsing。它不是一個確切的解決方案,但工程 – 2013-03-06 09:32:59

+0

這些部件不固定。這就是爲什麼這個模式是必需的。 – Manitoba 2013-03-06 09:37:01

回答

0

如果有人需要回答我的問題,這裏有使用模式:

Number: ^2b[0-9abcdefABCDEF]+2f545950453d504c4d4e$ 
Url: ^68747470[0-9abcdefABCDEF]+00$ 

,這裏是應用程序的MMS PDU:

public String getNumber(String pduString) 
{ 
    Pattern p = Pattern.compile("^2b[0-9abcdefABCDEF]+2f545950453d504c4d4e$"); 
    Matcher m = p.matcher(pduString); 
    return m.find() ? m.group(1) : ""; 
} 

public String getUrl(String pduString) 
{ 
    Pattern p = Pattern.compile("^68747470[0-9abcdefABCDEF]+00$"); 
    Matcher m = p.matcher(pduString); 
    return m.find() ? m.group(1) : ""; 
} 

請讓我知道這個代碼是否有問題。

PS:您可能需要將+添加到號碼才能使用國際格式,http://添加到網址。