|
[pre]
//---------------------------------------------------------------------------
#include void __fastcall TForm1::Button1Click(TObject *Sender) Memo2->Clear(); delete [] Buffer; void __fastcall TForm1::FormCreate(TObject *Sender) unsigned char __fastcall TForm1::HexToUChar(unsigned char symb) void __fastcall TForm1::PduCode(unsigned char * in, unsigned char * out)
E-mail:
info@telesys.ru
#include
#include
#pragma hdrstop
#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
{
Memo1->SelectAll(); //Выделяет весь набранный текст
int Size = Memo1->SelLength; //Определяет размер выделенного текста
Size++; //и увеличивает его на 1
unsigned char *Buffer = new unsigned char[Size]; //Создаёт динамический массив Buffer
unsigned char *out = new unsigned char[Size*2];
unsigned char *tmp, *tmp1;
tmp=out;
Memo1->GetSelTextBuf(Buffer, Size); //Размещает выбранный текст в Buffer
tmp1=_mbschr(Buffer, '\r');
if(tmp1) *tmp1=0;
if(RadioButton1->Checked)
{
PduCode(Buffer, out);
out=tmp;
while(*out)
Memo2->SelText=IntToHex(*out++, 2);
}
if(RadioButton2->Checked)
{
if(PduDecode(Buffer, out))
{
out=tmp;
while(*out)
Memo2->SelText=(char)*out++;
}
}
delete [] out;
}
//---------------------------------------------------------------------------
{
Memo1->SelectAll();
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::PduDecode(unsigned char * in, unsigned char * out)
{
if(StrLen(in)%2)
{
Application->MessageBox("Нечётное число символов в Hex представлении PDU", "ОШИБКА!", IDOK);
return false;
}
unsigned char *in_num = new unsigned char [StrLen(in)/2+1];
unsigned char *pin_num = in_num;
unsigned char *pout = out;
while(*in)
*pin_num++ = HexToUChar(*in++)<<4 | HexToUChar(*in++);
*pin_num = 0;
unsigned char count = 7, mask = 128;
unsigned char t1, t2;
while(*in_num)
{
if(count == 7)
{
*pout++ = *in_num & 127;
count = 0;
}
t1 = (*in_num++ & mask) >> (7 - count++);
t2 = *in_num << count;
*pout++ = (t1 | t2) & 127;
mask = (mask >> 1) + 128;
}
*pout = 0;
return true;
}
{
unsigned char result;
if((result = symb - 48)>9) result -= 7;
return result;
}
{
unsigned char mask=1;
unsigned char count=0;
while(*in)
{
if(count==7)
{
count=0;
mask=1;
*in++;
}
*out++=(*in>>count) | (*((in++)+1)&(mask))<<(7-count++);
mask=(mask<<1)+1;
}
*out=0;
}
[/pre]
Ответы