我做了一个短信发送的程序,但是,短信内容不能大于70个字节,如果大于70个字节的话就要每次截取70个字节来循环发送。
我写的代码只写了一半,如下:
string strOut="我做了一个短信发送的程序,但是,短信内容不能大于70个字节,如果大于70个字节的话就要每次截取70个字节来循环发送。";//这里不止70个字节了
byte[] bs=Encoding.Default.GetBytes(strOut);
if(bs.Length>70)
{
   这里我就不知道怎么截取了
}
是循环截取哦,要把strOut按每次70个字节的形式截取出来。
前两天我也发了同样的贴,但是他们回答的都是错的,也不是错,就是截取出来后有乱码出现
希望大家帮帮我,谢谢

解决方案 »

  1.   

    byte[] bs=Encoding.Default.GetBytes(strOut);
    if(bs.Length>70)
    {
       strOut.SubString(0,70);
    }
      

  2.   

    to : csharpxml()
    能说说怎么改吗?
      

  3.   


    byte[] temp;
    int pos = 0;
                
    while (pos < (bs.Length - 70))
    {
        temp = new byte[70];
        Array.Copy(bs, pos, temp, 0, 70);
        发送temp的内容
        pos += 70;
    }
    if(bs.Length-70>0)
    {
        temp = new byte[bs.Length-pos];
        Array.Copy(bs, pos, bs2, 0, bs.Length - pos);
        发送temp的内容
    }
      

  4.   

    byte[] bs=Encoding.Default.GetBytes(strOut);
    byte[] temp;
    int pos = 0;
    int left=bs.Length;   
    Encoding encoding =  Encoding.Default;while (left>0)
    {
        int tempCount=70;
        if (left > 70)
        {
            if (encoding.GetCharCount(bs, pos, 70) == 
                encoding.GetCharCount(bs, pos, 69) )
                tempCount = 69;
        }
        else
            tempCount = left;    temp = new byte[tempCount];
        Array.Copy(bs, pos, temp, 0, tempCount);
        //把temp发送出去
        left -= tempCount;
        pos += tempCount;
    }还有你确定手机也是用gb2312编码么
      

  5.   

    其实很简单:
    Encoding d = Encoding.Default;
    byte[] bs = d.GetBytes(strOut);
    strOut = d.GetString(bs, 0, 70);
      

  6.   

    while (Encoding.Default.GetByteCount(s) > 70)
    {
    byte[] b1 = Encoding.Default.GetBytes(s);
    byte[] b2;
    if (b1[71] > 0)
    {
    b2 = new byte[69];
    }
    else
    {
    b2 = new byte[70];
    }
    Array.Copy(b1, 0, b2, 0, b2.Length);
    string temp = Encoding.Default.GetString(b2);
    Console.WriteLine(temp);
    byte[] b3 = new byte[b1.Length - b2.Length];
    Array.Copy(b1, b2.Length, b3, 0, b3.Length);
    s = Encoding.Default.GetString(b3);
    }
    Console.WriteLine(s);
      

  7.   

    这个不行么??
    如果有乱码那是你编码方式的问题!!!
    Encoding.Default换成你字符串的编码方式
      

  8.   

    可以用Encoding.GetEncoding("编码名称");得到编码名称比如gb2312,Shift_JIS等
      

  9.   

    转换为utf-8(一个字符2个字节), 然后截取35个字符.
      

  10.   

    string strOut="我做了一个短信发送的程序,但是,短信内容不能大于70个字节,如果大于70个字节的话就要每次截取70个字节来循环发送。";//这里不止70个字节了
    string tmp;
    int start, len;
    start = 0;
    len = 70 /2;
    while( start < strOut.Length)
    {
    while(start + len < strOut.Length)
    {
    tmp = strOut.Substring(start, len);
    if(System.Text.Encoding.Default.GetByteCount(tmp) > 70)
    {
    len --;
    break;
    }
    else
    len ++;
    }
    if(start + len > strOut.Length)
    len = strOut.Length - start;
    byte[] bs=System.Text.Encoding.Default.GetBytes(strOut.Substring(start, len));
    start += len;
    len = 35;
    }
      

  11.   

    string strOut = "我做了一个短信发送的程序,但是,短信内容不能大于70个字节,如果大于70个字节的话就要每次截取70个字节来循环发送我做了一个短信发送的程序,但是,短信内容不能大于70个字节,如果大于70个字节的话就要每次截取70个字节来循环发送";
                
                byte[] bs=Encoding.Default.GetBytes(strOut);
                byte[] temp;
                int pos = 0;
                int left=bs.Length;
                Decoder decoder = Encoding.Default.GetDecoder();
                
                int c1, c2;
                
                while (left>0)
                {
                    int tempCount=70;
                    if (left > 70)
                    {
                        c1=decoder.GetCharCount(bs, pos, 70); 
                        c2=decoder.GetCharCount(bs, pos, 69);
                        if (c1== c2)
                            tempCount = 69;
                    }
                    else
                        tempCount = left;                temp = new byte[tempCount];
                    Array.Copy(bs, pos, temp, 0, tempCount);
                    //把temp发送出去
                    left -= tempCount;
                    pos += tempCount;
                }
    前两天有点小错,这个测试过了