600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > PDU串发送长短信

PDU串发送长短信

时间:2023-10-04 23:13:00

相关推荐

PDU串发送长短信

PDU串发送长短信

Code

1

/**//// <summary>

2

/// 针对国内短信编码(USC2)

3

/// </summary>

4

public class PDUdecoding

5

{

6

public readonly static int MAX_CHAR_COUNT = 70;//最长可发送汉字个数

7

8

/**//// <summary>

9

/// 奇偶互换并补F

10

/// </summary>

11

/// <param name="value"></param>

12

/// <returns></returns>

13

private static string ParityChange(string value)

14

{

15

string result = string.Empty;

16

int length = value.Length;

17

for (int i = 1; i < length; i += 2)//奇偶互换

18

{

19

result += value[i];

20

result += value[i - 1];

21

}

22

23

if (!(length % 2 == 0))//不是偶数则加上F,与最后一位互换

24

{

25

result += "F";

26

result += value[length - 1];

27

}

28

return result;

29

}

30

31

/**//// <summary>

32

/// 短信内容编码

33

/// </summary>

34

/// <param name="value"></param>

35

/// <returns></returns>

36

/// <remarks>

37

/// 采用Big-Endian 字节顺序的 Unicode 格式编码,将高低位互换

38

/// 将转换后的短信内容存进字节数组

39

/// 去掉在进行Unicode格式编码中,两个字节中的"-",例如:00-21,变成0021

40

/// 将整条短信内容的长度除2,保留两位16进制数

41

/// </remarks>

42

public static string Encoding(string value)

43

{

44

Encoding encoding = System.Text.Encoding.BigEndianUnicode;

45

string result = string.Empty;

46

byte[] bytes = encoding.GetBytes(value);

47

48

for (int i = 0; i < bytes.Length; i++)

49

{

50

result += BitConverter.ToString(bytes, i, 1);

51

}

52

53

return result;

54

}

55

56

public static string EncodeingAddLength(string value)

57

{

58

string result = Encoding(value);

59

return String.Format("{0:X2}{1}", result.Length / 2, result);

60

}

61

62

/**//// <summary>

63

/// 短信中心号码编码

64

/// </summary>

65

/// <param name="value"></param>

66

/// <returns></returns>

67

public static string DecodingCenter(string phone)

68

{

69

string result = string.Empty;

70

result = ParityChange(phone);

71

72

result = String.Format("91{0}", result);//加上91

73

result = String.Format("{0:X2}{1}", result.Length / 2, result);

74

return result;

75

}

76

77

/**//// <summary>

78

/// 接收短信手机号码编码

79

/// </summary>

80

/// <param name="phone"></param>

81

/// <returns></returns>

82

public static string DecodingPhone(string phone)

83

{

84

string result = string.Empty;

85

86

if (null == phone || 0 == phone.Length)

87

{

88

return result;

89

}

90

91

if ("+" == phone[0])

92

{

93

phone = phone.TrimStart("+");

94

}

95

96

if (!(phone.Substring(0, 2) == "86"))//补86

97

{

98

phone = String.Format("86{0}", phone);

99

}

100

101

return ParityChange(phone);

102

}

103

104

/**//// <summary>

105

/// 整个短信的编码

106

/// </summary>

107

/// <param name="center"></param>

108

/// <param name="phone"></param>

109

/// <param name="content"></param>

110

/// <param name="length">要发送内容的长度,由两部分组成,接收手机号加上要发送的内容</param>

111

/// <returns></returns>

112

public static string EncodingSMS(string center, string phone, string content, out string length)

113

{

114

center = DecodingCenter(center);

115

string result = String.Format("{0}11000D91{1}000800{2}", center, DecodingPhone(phone), EncodeingAddLength(content));

116

length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

117

return result;

118

}

119

120

/**//// <summary>

121

/// 超长短信

122

/// </summary>

123

/// <param name="center"></param>

124

/// <param name="phone"></param>

125

/// <param name="content"></param>

126

/// <param name="count"></param>

127

/// <param name="i"></param>

128

/// <param name="length"></param>

129

/// <returns></returns>

130

public static string EncodingSMS(string center, string phone, string content,int count,int i, out string length)

131

{

132

if (content.Length <= PDUdecoding.MAX_CHAR_COUNT)

133

{

134

return PDUdecoding.EncodingSMS(center, phone, content, out length);

135

}

136

else

137

{

138

if (count - 1 == i)

139

{

140

content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT-6));

141

}

142

else

143

{

144

content = content.Substring(i * (PDUdecoding.MAX_CHAR_COUNT-6), PDUdecoding.MAX_CHAR_COUNT-6);

145

}

146

147

center = DecodingCenter(center);

148

content = Encoding(content);

149

150

string result = "";

151

152

DateTime tm = DateTime.Now;

153

154

//result = string.Format("{0}44000D91{1}0008{2}{3:X2}05000304{4:D2}{5:D2}{6}",

155

// center,

156

// DecodingPhone(phone),

157

// ParityChange(string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}08", tm.Year - 2000, tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second)),

158

// (content.Length + 12) / 2,

159

// count,

160

// i + 1,

161

// content

162

// );

163

164

result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",

165

center,

166

DecodingPhone(phone),

167

(content.Length + 12) / 2,

168

count,

169

i + 1,

170

content);

171

172

length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

173

return result;

174

}

175

}

176

177

public static int GetMaxEncodeCharCount()

178

{

179

return PDUdecoding.MAX_CHAR_COUNT * 2 - 6;

180

}

181

/**//// <summary>

182

///

183

/// </summary>

184

/// <param name="center"></param>

185

/// <param name="phone"></param>

186

/// <param name="count"></param>

187

/// <param name="i"></param>

188

/// <param name="content"></param>

189

/// <param name="length"></param>

190

/// <returns></returns>

191

public static string EncodingSMS(string center, string phone, int count, int i, string content, out string length)

192

{

193

string message = string.Empty;

194

int msgcount = PDUdecoding.GetMaxEncodeCharCount() * 2;//固定

195

196

if (i == count)

197

{

198

message = content.Substring((i - 1) * msgcount); //共可发送134个编码

199

}

200

else

201

{

202

message = content.Substring((i - 1) * msgcount, msgcount);

203

}

204

205

center = DecodingCenter(center);

206

207

//string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",

208

// center,

209

// DecodingPhone(phone),

210

// (message.Length + 12) / 2,

211

// count,

212

// i,

213

// message);

214

215

//length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

216

217

/**/////string ph = phone;

218

////phone = DecodingPhone(phone).Substring(2);

219

220

////string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",

221

//// string.Format("{0:X2}A1", ph.Length),

222

//// phone,

223

//// (message.Length + 12) / 2,

224

//// count,

225

//// i,

226

//// message);

227

228

string result = String.Format("{0}51000D91{1}000800{2:X2}05000304{4:D2}{5:D2}{3}",

229

center,

230

DecodingPhone(phone),

231

message.Length/2 + 6,

232

message,

233

count,

234

i

235

);

236

237

length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

238

239

return result;

240

}

241

242

public static string EncodingOther(string content)

243

{

244

string result = string.Empty;

245

byte[] bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(content);

246

247

//for (int i = 0; i < bytes.Length; i++)

248

//{

249

// if (0x80 == (bytes[i] & 0x80))//汉字 6c49 108 73

250

// {

251

// result = string.Format("{0}{1:X2}", result, bytes[i]);

252

// result = string.Format("{0}{1:X2}", result, bytes[++i]);

253

// }

254

// else

255

// {

256

// result = string.Format("{0}00{1:X2}", result, bytes[i]);

257

// }

258

//}

259

foreach (char item in content)

260

{

261

bytes = System.Text.Encoding.BigEndianUnicode.GetBytes(new char[1]

{item});

262

if (bytes.Length < 2)

263

continue;

264

265

//

266

if (0x80 ==( Asc(item) & 0x80))//汉字

267

{

268

result = string.Format("{0}{1:X2}", result, bytes[0]);

269

result = string.Format("{0}{1:X2}", result, bytes[1]);

270

}

271

else

272

{

273

result = string.Format("{0}00{1:X2}", result, bytes[1]);

274

}

275

}

276

return result;

277

}

278

279

private static int Asc(char item)

280

{

281

byte[] bytes = System.Text.Encoding.Default.GetBytes(new char[1]

{ item });

282

283

if (bytes.Length < 2)

284

return bytes[0];

285

286

return bytes[0] * 256 + bytes[1] - 65535;

287

}

288

}

发送长短信时需要先调用EncodingOther对字符进行编码,然后调用EncodingSMS第二个重载方法发送。

发送长短信时需要调用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。