Base64与7位GSM编码不兼容?

时间:2011-08-02 23:42:57

标签: android sms base64 7-bit

由于我无法在160个字符内压缩长度为256字节的长md5字符串,因此我决定在Android中使用多部分消息将SMS分成几个部分,接收方将收听短信然后将这些消息合并消息。

我使用此处的组合算法:Android - receiving long SMS (multipart)

经过几次测试后,我发现我发送的短信完全搞砸了,虽然我在发送之前用Base64对它们进行了编码。

认为SMS是一种7位编码方法,我认为这可能是编码问题。

但我已经成功发送了Base 64编码的消息,但它很短并且在1条短信内。

我的问题是:

如果是编码问题,为什么我可以在160个字符内发送完全可读的Base64编码消息,但在发送超过160个字符的消息时无法获得可读结果?

我在这里附上了我的代码: 我打算发送的字符串:

static final String bigNum = "C196BA05AC29E1F9C3C72D56DFFC6154A033F1477AC88EC37F09BE6C5BB95F51C296DD20D1A28A067CCC4D4316A4BD1DCA55ED1066D438C35AEBAABF57E7DAE428782A95ECA1C143DB701FD48533A3C18F0FE23557EA7AE619ECACC7E0B51652A8776D02A425567DED36EABD90CA33A1E8D988F0BBB92D02D1D20290113BB562CE1FC856EEB7CDD92D33EEA6F410859B179E7E789A8F75F645FAE2E136D252BFFAFF89528945C1ABE705A38DBC2D364AADE99BE0D0AAD82E5320121496DC65B3930E38047294FF877831A16D5228418DE8AB275D7D75651CEFED65F78AFC3EA7FE4D79B35F62A0402A1117599ADAC7B269A59F353CF450E6982D3B1702D9CA83";

守则

private void sendMsg (String phoneNum, String msg) {
    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(msg);
    sms.sendMultipartTextMessage(phoneNum, null, parts, null, null);
}

public void onStart() {
    super.onStart();
    final String bigNum64 = Base64.encodeToString(bigNum.getBytes(), 0);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sendMsg("5554", bigNum64);
            textView.setText(bigNum64);
        }
    });
}

public void onResume() {
    super.onResume();
    Bundle receiver = getIntent().getExtras();
    if (receiver != null) {
        String msg = receiver.getString("SMS");
        textView.setText("Received" + msg);
    }
}
}

SMS接收器:

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    Object[] pdus = (Object[]) bundle.get("pdus");
    SmsMessage[] messages = new SmsMessage[pdus.length];
    String body = "";
    for (int i = 0; i < pdus.length; i++) {
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
    }
    SmsMessage sms = messages[0];
    try {
        if (messages.length == 1 || sms.isReplace()) {
            body = sms.getDisplayMessageBody();

        }
        else {
            StringBuilder bodyText = new StringBuilder();
            for (int i = 0; i < messages.length; i++) {
                bodyText.append(messages[i].getMessageBody());
            }
            body = bodyText.toString();
        }
    }
    catch (Exception e) {

    }
    Intent start = new Intent(context, SendLongSMSActivity.class);
    start.putExtra("SMS", body);
    start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(start);
}

感谢您的帮助!

相关文章:Any way to compress a 256 bytes "MD5-like" string into 160 bytes or less?

即使我提供了一个相当简单的长字符串,例如

"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"

无论是否使用Base64编码,接收器都无法获得正确的结果。

更新#2

当使用真实手机进行测试时,接收器可以在没有基座64的情况下接收正确的消息,这是由于模拟器使文本乱码

问题现已结束。

感谢所有帮助的人!而且@x @Dan

更新

字符串“0123456789ABCDEF ...”没有base64,分为两部分:

part [0]    "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345678" 

part [1]    "9ABCDEF0123456789ABCDEF"   

这是“0123456789ABCDEF ...:

的分隔的base64字符串
part[0] "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4\nOUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAx"    

part[1] "\nMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlB\nQkNERUY=\n"    

1 个答案:

答案 0 :(得分:0)

sms除法似乎是在每77个字符后添加\ n个字符,最后,只需从字符串中删除它们,它就会正确解码。