-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.cpp
212 lines (161 loc) · 5.14 KB
/
Unit1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <string.h>
#include <iostream> // std::cout
#include <sstream>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
// Ïðåîáðàçîâàíèå íîìåðà â íóæíûé (PDU) è îáðàòíî (òåêñòîâûé)
String TForm1::workNumber(String number, String type){
if(type == "uncode"){
// Óäàëÿåì 1 ñèìâîë +
number.Delete(1,1);
// Åñëè ÷èñëî íå÷åòíîå, äî äîáàâëÿåì F â êîíöå
if(number.Length() % 2 == 1)
number = number + "F";
}
// Ìåíÿåì ÷èñëà ìåñòàìè
int i = 1;
char swap[1];
while(i < number.Length()){
swap[0] = number[i];
number[i] = number[i+1];
number[i+1] = swap[0];
i = i + 2;
}
// Óäàëÿåì F, åñëè ÷èñëî íå÷åòíîå
if(type == "decode")
if(number[ number.Length() ] == 'F')
number.Delete( number.Length() ,1);
// Äîáàâëÿåì + â íà÷àëî, åñëè äåêîäèðóåì
if(type == "decode")
number = "+" + number;
return number;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String text;
String textNew;
String fullStr;
// Ïðîâåðêà íà ââîä äàííûõ
if(Edit1->Text == "" || Edit1->Text.Length() < 12){
Label4->Caption = "Íåâåðíûé ôîðìàò íîìåðà.";
return;
}
// Ïðîâåðêà íà ââîä òåêñòà
if(Edit2->Text == "" || Edit1->Text.Length() < 1){
Label5->Caption = "Íå ââåäåí òåêñò.";
return;
}
// Î÷èùàåì âñå
Label4->Caption = "";
Label5->Caption = "";
text = "";
textNew = "";
fullStr = "";
Memo2->Clear();
String Recipient = "";
// Ôîðìèðóåì íîìåð
String EncodeNum = workNumber(Edit1->Text, "uncode");
// Êîë-âî ñèìâîëîâ â íîìåðå
if(EncodeNum[ EncodeNum.Length() -1 ] == 'F')
Recipient = IntToHex( EncodeNum.Length()-1 , 2) + "91" + EncodeNum;
else
Recipient = IntToHex( EncodeNum.Length() , 2) + "91" + EncodeNum;
// Òåêñò ñîîáùåíèÿ
text = Edit2->Text;
// Ìàññèâ ñ áàéòàìè ñèìâîëîâ
TBytes FirstLine;
FirstLine = TEncoding::Unicode->GetBytes(text);
// Ôîðìèðóåì ìàññèâ áàéòîâ äëÿ PDU
for(int i = 0; i < FirstLine.Length; i+=2)
textNew = textNew + IntToHex(FirstLine[i+1], 2) + "" + IntToHex(FirstLine[i], 2);
// Âûâîäèì ïîëíûé ðåçóëüòàò PDU ñòðîêè
Memo2->Lines->Add("000100" + Recipient + "000812" + "" + textNew);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, System::WideChar &Key)
{
// Ââîä òîëüêî ÷èñåë è backspace
if( Key != 8 && ( (Key < '0' || Key > '9' ) ) ) Key = NULL;
else if( Key == 8 && Edit1->Text.Length() == 1 && Edit1->Text[1] == '+')
Key = NULL;
return;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Ïîëó÷àåì ñîäåðæàíèå
String pduFull = Memo1->Lines->GetText();
// Ïðîâåðêà íà ïóñòàòó è êîë-âî ñèìâîëîâ
if(pduFull == ""){
Label8->Caption = "Âû íå ââåëè PDU.";
return;
}
else if(pduFull.Length() < 28){
Label8->Caption = "Íåâåðíûé ôîðìàò PDU.";
return;
}
Label8->Caption = "";
Edit3->Text = "";
Edit4->Text = "";
int countNum = 0;
// Óäàëÿåì ïåðâûå ñëóæåáíûå äàííûå
pduFull.Delete(1,6);
// Ïåðåìåííàÿ äëÿ íîìåðà
String numberMob;
// Ïåðåâîäèì 16 ðè÷íóþ â 10 ÷íóþ (êîë-âî ñèìâîëîâ â òåëåôîíå)
AnsiString countNumber = pduFull.SubString(1,2);
std::string hex;
hex = countNumber.c_str();
std::istringstream stream(hex);
stream >> std::hex >> countNum;
// Óäàëÿåì èíôîðìàöèþ îá êîë-âî ñèìâîëîâ è
pduFull.Delete(1,4);
// Åñëè
if(countNum % 2 == 1)
countNum = countNum + 1;
// Âûòàñêèâàåò íîìåð
for(int i = 1; i <= countNum; i++)
numberMob = numberMob + pduFull[i];
// Ïîëó÷àåì íîðìàëüíûé íîìåð è âñòàâëÿåì åãî
Edit3->Text = workNumber(numberMob, "decode");
// Âûðåçàåì íîìåð è îñòàâëÿåì òîëüêî òåêñò
pduFull.Delete(1,6+countNum);
// Ñòðîêà äëÿ ïåðåäåëûâàåíèÿ â ñèìâîë
String textConv;
// Ïåðåáèðàåì êîäû ñèìâîëîâ â ñàìè ñèìâîëû
for(int i = 1; i <= pduFull.Length()-2; i+=4){
textConv = (String)"0x" + (String)pduFull[i] + (String)pduFull[i+1] + (String)pduFull[i+2] + (String)pduFull[i+3];
Edit4->Text = Edit4->Text + wchar_t( StrToInt(textConv) );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
// Âñòàâëÿåì + â íà÷àëî (åñëè óäàëåíî)
if(Edit1->Text.Length() == 0)
Edit1->Text = '+';
else if(Edit1->Text.Length() > 1 && Edit1->Text[1] != '+'){
String textNumCheck = Edit1->Text;
for(int i = 1; i <= textNumCheck.Length(); i++){
if(textNumCheck[i] == '+'){
textNumCheck.Delete(i,1);
break;
}
}
Edit1->Text = "+" + textNumCheck;
}
}
//---------------------------------------------------------------------------