首页 > 解决方案 > 使用 GSM-7 和 UCS-2 的 SMS 拆分字符串

问题描述

我想弄清楚如何获取一个可能包含或不包含 unicode(例如表情符号)的字符串,并将其正确拆分为可以通过 SMS 发送的“部分”。我使用多个不同的 SMS API,不能依赖所有这些 API 来连接相同(或正确),所以我宁愿自己负责拆分消息。此外,我想实现某种自动换行,以便当消息被强制拆分时,我不会将一个词切成两半,而是将其推送到下一条短信。

据我了解,SMS 限制为 160 个字符,或者在串联的情况下限制为 153 个,但这严格使用 GSM-7 的 7 位字符,并且在 UCS-2 中添加 unicode 字符会使字符限制显着下降,因为UCS-2 编码字符占用更多字节。因此,计数字符似乎是不可靠的,相反,我需要以某种方式拆分字节而不破坏消息。

我试图弄清楚如何写:

- get bytes of msgBody string
- get encoding type of msgBody, GSM-7 or UCS-2
- get max byte size of that encoding type
- if msgBody byte size = multiple messages, split into parts
- if first part's last character isn't whitespace, push the cut-up word's first half into the next message
- somehow insure that pushing those bytes along doesn't result in the next message being too large

标签: c#.netvb.netcharacter-encodingsms

解决方案


推荐阅读