Base64 与 7 位 GSM 编码不兼容?

发布于 2024-11-27 08:56:40 字数 4263 浏览 1 评论 0原文

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

我使用这里的组合算法: Android - receive long SMS (multipart)

之后经过一些测试,我发现我发送的短信完全混乱,尽管我在发送之前用 Base64 对其进行了编码。

认为短信是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);
    }
}
}

短信接收者:

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);
}

感谢您的帮助!

相关文章:有什么方法可以将 256 字节的“类似 MD5”字符串压缩为 160 字节或更少吗?

即使我给出一个相当简单的长字符串,例如

"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"

,无论是否有 Base64编码后,接收方无法得到正确的结果。

更新 #2

使用真机测试时,接收方可以在没有 Base 64 的情况下收到正确的消息,这是由于模拟器乱码了文本

问题现已结束。

感谢所有提供帮助的人!还有@Dan

更新:

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

part [0]    "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345678" 

part [1]    "9ABCDEF0123456789ABCDEF"   

这是“0123456789ABCDEF...”的划分的base64字符串:

part[0] "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4\nOUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAx"    

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

Since I am not able to compress a 256-byte long md5-like string within 160 characters, I decided to use Multi-part message in Android to divide the SMS into several parts and the receiver will listen the SMS then combine those messages.

I use the combination algorithm from here: Android - receiving long SMS (multipart)

And after a few testing I found that the SMS I sent was totally messed, though I encoded them with Base64 before sending.

Thinking the SMS is a 7-bit Encoding method I thought it might be the encoding problem.

But I have successfully sent Base 64 encoded message, but it was short and within 1 SMS.

My question is:

If it is a encoding problem, then why I can send a totally readable Base64 encoded messages within 160 characters, but cannot get a readable result when sending messages exceeding 160 characters?

I've attached my code here:
The string I intend to send:

static final String bigNum = "C196BA05AC29E1F9C3C72D56DFFC6154A033F1477AC88EC37F09BE6C5BB95F51C296DD20D1A28A067CCC4D4316A4BD1DCA55ED1066D438C35AEBAABF57E7DAE428782A95ECA1C143DB701FD48533A3C18F0FE23557EA7AE619ECACC7E0B51652A8776D02A425567DED36EABD90CA33A1E8D988F0BBB92D02D1D20290113BB562CE1FC856EEB7CDD92D33EEA6F410859B179E7E789A8F75F645FAE2E136D252BFFAFF89528945C1ABE705A38DBC2D364AADE99BE0D0AAD82E5320121496DC65B3930E38047294FF877831A16D5228418DE8AB275D7D75651CEFED65F78AFC3EA7FE4D79B35F62A0402A1117599ADAC7B269A59F353CF450E6982D3B1702D9CA83";

The Code

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);
    }
}
}

The SMS Receiver:

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);
}

Thanks for your help!

Related Post: Any way to compress a 256 bytes "MD5-like" string into 160 bytes or less?

Even if I give a rather simple long string, like

"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"

and no matter with or without Base64 encoding, the receiver cannot get the correct results.

UPDATE #2

When using real phone for testing, the receiver can receive correct message without base 64, it was due to the emulator garbled the text.

Question now closed.

Thanks for everyone who helps! And thanx @Dan

UPDATE:

The string "0123456789ABCDEF..." without base64, divided into 2 parts:

part [0]    "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345678" 

part [1]    "9ABCDEF0123456789ABCDEF"   

Here's the divided base64 string of the "0123456789ABCDEF...:

part[0] "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4\nOUFCQ0RFRjAxMjM0NTY3ODlBQkNERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCQ0RFRjAx"    

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-12-04 08:56:40

看来 SMS 划分是在每 77 个字符后添加 \n 字符,最后,只需从字符串中删除这些字符,它就会正确解码。

It appears that the sms divide is adding \n characters after every 77 characters and at the end, just strip those from your string and it will decode properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文