c++builder 字节 编码 转换大全 String TBytes byte

System.SysUtils

System::DynamicArray<System::WideChar> TCharArray

System::TArray__1<System::WideChar> TCharArray;

TCharArray

String TBytes byte

编码类型有:ASCII、8BIT、7BIT、UCS2-BIG、UCS2-LIT、UCS2-80、UCS2-81、UCS2-82、UTF-8、UTF-16(Unicode)

RawToBytes

BytesToRaw

Byte一个字节,能存放0..255,就是unsigned char,小写的byte等价于大写的Byte。

array

TByteDynArray= array of Byte;

TIdBytes= array of Byte;

typedef System::DynamicArray<System::Byte> TIdBytes;

typedef System::DynamicArray<System::Byte> TBytes;

动态数组的写法

DynamicArray<Byte> TBytes;

DynamicArray<int> TInts;

TBytes = TArray<Byte>;

String> TBytes/TByteDynArray

Text.BytesOf()

BytesOf

WideBytesOf()

TBytes bv;
    bv.set_length(8);

bytes=System::Sysutils::BytesOf(Caption);
bytes=System::Sysutils::WideBytesOf(Caption);//value is spec wide

bytes=Caption.BytesOf();

TBytes/TByteDynArray > String

Caption = System::Sysutils::StringOf(barr);
Caption = System::Sysutils::WideStringOf(barr);

String > <Utf8Bytes

TBytes bytes;
 bytes = TEncoding::UTF8->GetBytes(Memo1->Text);

Memo1->Text = TEncoding::UTF8->GetString(bytes);

delphi

TEncoding.UTF8.GetBytes

TBytes/TByteDynArray > Byte *

TBytes bytes;

Byte * bt = &bytes[0];

byte bb[255];

或者

bt = new Byte[bytes.Length];
 memcpy(bt,&bytes[0],bytes.Length);

TBytes初始化memset

TBytes bv;
        bv.set_length(5000);

memset(     &bv[0], 0,bv.Length );

Byte * >TBytes/TByteDynArray

Byte * bt;

TBytes bytes;

bytes = BytesOf(bt, sizeof(bt));

UnicodeString s1;

s1 = "Hello World!";
s1.Length(), ByteLength(s1)

ByteLength

TIdBytes

TIdBytes = array of Byte;

TBytes = TArray<Byte>;

uses IdGlobal;//head file

TIdBytes>String

String  str=BytesToString( AValue: TIdBytes);

TIdBytes>TBytes

BytesToRaw(Buffer,abuffer,Length(Buffer));

SetLength(bs,0);
SetLength(bs,Length(idbs));

BytesToRaw(idbs,bs,Length(idbs));//error

BytesToRaw(idbs,bs[0],Length(idbs));//ok

buf :=TBytes( idbus);//ok

TBytes>TIdBytes

function RawToBytes(const AValue; const ASize: Integer): TIdBytes;

idbs := RawToBytes(bs[0], Length(bs));//必须加下标0,否则就是错是

idbs := RawToBytes(bs, Length(bs));//没有下标0是错的

结构体>TIdBytes

RawToBytes(SendData,SizeOf(SendData));//SendData是结构体变量,不要下标0

UTF8String Bytes>UnicodeString

字节转换为16进制

idbytes to hexString

String hexstr;
for (int i = idbytes.Low; i <= idbytes.High; i++)
{
hexstr += IntToHex(idbytes[i], );
}

delphi

PAnsiChar数组转为字符串

R: array[0..19] of AnsiChar;

sss:ansistring;

astr := AnsiString(r);

StrPCopy(r,astr);

Delphi7升级

AnsiChar(DelphiXE10)= Char(Delpih7)

Delphi7:

Object: array[1..6] of Char;

DelphiXE10,Berlin

Object: array[1..6] of AnsiChar;

sbytes := VarArrayOf([$AA, $EE, $81, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0]);

AnsiString>Bytes>AnsiString

bsa := TEncoding.ANSI.GetBytes(as1);
 as1:= TEncoding.ANSI.GetString(bsa);

假UTF8Sring转换为汉字

UTF8String s8="闫换珍";

AnsiString s8a="闫换珍";

都是6个字节,一个汉字2个字节。

c++builder 字节 编码 转换大全 String TBytes byte

此时,想还原汉字this->Caption=???如何才能显示汉字

AnsiString  as21 = s8;//err
AnsiString as22 = AnsiString(s8);//err
AnsiString as2 = s8.c_str();//ok String s991 = s8;//err
String s992 = String(s8);//err
String s99 = s8.c_str();//ok 最可靠

as1:= TEncoding.ANSI.GetString(bs8);//ok

真UTF8String

UTF8String s8=L"闫换珍";//加上L标识才是真正的UTF8,一个汉字3个字节,UnicodeString函数是2个字节。

AnsiString s8a="闫换珍";

TBytes bs81 =TEncoding::UTF8->GetBytes(s8);
TBytes bs82 =TEncoding::UTF8->GetBytes(s8a);

两个bytes里是相等的,正确的。

char dest[256] = {0}; // room for 256 characters
    UnicodeString src = L"闫换珍";
    UnicodeToUtf8(dest, src.c_str(), 256);

//或者     UnicodeToUtf8(dest,256, src.c_str(), src.Length*();

dest与bs81,bs82值相等。

RawByteString srbRaw;
    srbRaw = UTF8Encode(ss);
    //ss = UTF8Decode(srb);
    char  Buf[512] = {0    };
    strcpy(Buf, srbRaw.c_str());

Buf这个也是正确的utf8字节

UTF8String>String

   String  s=s8;

UnicodeString us1 =s8;
    UnicodeString us11 = UTF8ToUnicodeString(s8.c_str());
    UnicodeString us13 = UTF8ToString(s8.c_str());
    UnicodeString us14 = Utf8ToAnsi(s8);
    RawByteString rbs1 = AnsiToUtf8(s8);

word/short/int 简单类型转换为bytes

var
  Buf:TBytes;
  w:Word;
begin
  w:=10;
  Buf:=RawToBytes(w);
end;
var
  Buf:Array[0..1of Byte;
  w:Word;
begin
  w:=10;
  Move(w,Buf[0],2);

end;

doule>Byte[]

byte bt[8];

double ad;
memcpy(&bt[0], (byte*)&ad, 8);

memcpy(&bt[0], (byte*)&ajava, sizeof(double));

#if defined(USE_ENCODING)
TBytes FirstLine; // A dynamic array of bytes
FirstLine.Length = ;
stream = _wfopen(OpenDialog1->Files->Strings[I].c_str(), L"r");
fgets(&FirstLine[], FirstLine.Length, stream);
Memo1->Lines->Append(TEncoding::UTF8->GetString(FirstLine));
fclose(stream);
#else
char FirstLine[];
stream = _wfopen(OpenDialog1->Files->Strings[I].c_str(), L"r");
fgets(FirstLine, sizeof(FirstLine), stream);
Memo1->Lines->Append(UTF8String(FirstLine));
fclose(stream);
#endif

delphi

LBuffer: TBytes;

SetLength(LBuffer, LFileStream.Size);

LFileStream.ReadBuffer(Pointer(LBuffer)^, Length(LBuffer));

C++builder VCL String

String astr="hello";

astr[0]//error,下标正确的是从1开始

StringToOleStr

字符串函数

String ss;

ss.LastDelimiter(" ");

最后一个空格出现的位置,这样就不用循环了

BSTR  bs1;

fun(BSTR *name);

fun(&bs1);

Delphi Berlin 有此函数,int转换为16进制

ai:Integer;
ai:=17;
self.Caption:=ai.ToHexString;

StringList>字符数组

arr:TArray<string>;

list:TStringList;

arr := list.ToStringArray;

TStringDynArray

TMemoryStream>TBytes

LResponse: TMemoryStream;

unsigned char *>AnsiString

AnsiString str1= (char *)buff;

AnsiString str2((const char *)buff);

AnsiString>unsigned char*

strcpy(uchar,AnsiString(str).c.str());

ascii码转16进制

String Asc2Hex(String astr)
{
TBytes bytes;
bytes = TEncoding::ASCII->GetBytes(astr);
String hexstr;
for (int i = bytes.Low; i <= bytes.High; i++)
{
hexstr += IntToHex(bytes[i], );
} return hexstr;
}
String Hex2Asc(String hexStr)
{
int nLen = hexStr.Length();
int j = ;
String ahex;
char achar;
String retstr;
if ( != (nLen % ))
{
return -;
}
for (int i = ; i < nLen; i = i + , j++)
{
ahex = hexStr.SubString(i, );
ahex = "0x" + ahex;
achar = char(ahex.ToInt());
retstr = retstr + achar; }
return retstr;
}

UnicodeString>std:string

String at="abc";

std:string sstr = AnsiString(at).c_str();

字符串数组

var  arr:TArray<string>;

TStringDynArray 

ArrayOfString2 = array of string;

string2Bytes
String astr = "中国";
TBytes b1,b2,b3,b4;
b1 = TEncoding::UTF8->GetBytes(astr);
b2 = TEncoding::ANSI->GetBytes(astr);
b3 = TEncoding::Default->GetBytes(astr);
b4 = TEncoding::Unicode->GetBytes(astr);
E4B8ADE59BBD
D6D0B9FA
D6D0B9FA
2D4EFD56 高地位修正后:4E2D 56FD ,也有这样表单\u4e23 \u56fd String s1,s2,s3,s4; s1 = TEncoding::UTF8->GetString(b1);//中国
s2 = TEncoding::ANSI->GetString(b2);//中国
s3 = TEncoding::Default->GetString(b3);//中国
s4 = TEncoding::Unicode->GetString(b4);//中国 bytes数组转换成HexString
String bytesToHex(TBytes data)
{
   String hexstr;
for (int i = 0; i < data.Length; i++)
hexstr+= IntToHex( data[i],2);
return hexstr;
} 特殊汉字
char=13,keypress,
包含key=13:复 包含tab
delphiAnsiChar array 转换为 tbytes
bts:tbytes;
outbuf: array[0..1000] of AnsiChar; setlength(bts,200);
Move(bts[0[, outbuf[0], 200);
https://www.cnblogs.com/del88/p/5448317.html
bytesof(str)
stringof(bytes) T

strAnsi:= PAnsiChar(AnsiString(str));

TBytes -> PChar :     LPChar :=  PChar(LTBytes);

PChar -> TBytes:LTBytes := BytesOf(LPChar);

TBytes -> Array of AnsiChar:move(LTBytes[i],LArrayOfAnsiChar[k]);

Array of AnsiChar -> string:LString := StringOf(BytesOf(LArrayOfAnsiChar));

https://www.cnblogs.com/keynexy/p/5919962.html
 
上一篇:C#2.0 Socket套接字编程之实例初探 200


下一篇:mybatis实战教程(mybatis in action)之九:mybatis 代码生成工具的使用