Java字节数组德尔福字节ARRY [英] Java byte array to Delphi byte arry
问题描述
我的工作在Android蓝牙打印。打印机是标准的POS热敏打印机。
这是我的方式将数据发送到打印机(本部分使用正常):
VAR
适配器:JBluetoothAdapter;
远端设备:JBluetoothDevice;
袜子:JBluetoothSocket;
ostream的:JOutputStream;
开始
适配器:= TJBluetoothAdapter.JavaClass.getDefaultAdapter;
远端设备:= Adapter.getRemoteDevice(stringtojstring(的TargetMAC));
袜子:= remoteDevice.createRfcommSocketToServiceRecord(UID);
ostream的:= Sock.getOutputStream; ....
功能StringToJA(数据:字符串):TJavaArray<字节取代;
VAR
X:整数;
LEN:整数;
开始
LEN:=长度(数据);
结果:= TJavaArray<&字节GT; .Create(LEN);
为X:= 0到LEN做
开始
Result.Items [X]:=奥德(数据[X]);
结束;
结束;
//这行式打印文本
ostream.write(StringToJA(TXT));
现在我想要加载不同的字符encoing,这是JAVA code:
int类型的= 23;
字符串codeL =ISO8859-2
字符串的txt =SOME TEXT ... SCZ;
字节的数据[] = txt.getBytes(codeL);
字节sendText [] =新字节[3];
sendText [0] = 0x1B;
sendText [1] = 0x74;
sendText [2] =(字节)一个;
mobileprint.PRTSendBuffer(sendText,sendText.length);
我怎样才能将这些行?
- txt.getBytes(codeL);
- sendText [2] =(字节)一个;
德尔福XE5 code:
...
答:整数;
TXT,codeL:字符串;
数据:TJavaArray<字节取代;
sendText:TJavaArray<字节取代;
开始
答:= 23;
codeL:='ISO8859-2';
TXT:='SOME TEXT ... SCZ';
数据:=? txt.getBytes(codeL); ???
sendText:= TJavaArray<&字节GT; .Create(3);
sendText.Items [0] = $ 1B;
sendText.Items [1]:= $ 74条;
sendText.Items [2]:= ???字节(A)???; ostream.write(sendText);
ostream.write(数据);
这是JAVA防爆pressions值从Exlipse:
有关X:= 0到LEN做
块引用>这应该使用
LEN-1
,否则你会走出数组和字符串两者的界限。您还没有采取统一code考虑复制字符串的字节数组的时候,所以你可能会遇到的问题,如果
字符串
包含任何需要使用UTF-16替代物的非ASCII字符。您应该使用SysUtils.TEncoding
类的<code>字符串先转换为德尔福字节数组,然后复制到在Java字节数组,例如:函数StringToJA(数据:字符串):TJavaArray&LT;字节取代;
VAR
编曲:TB的;
LEN,X:整数;
开始
编曲:= TEncoding.Default.GetBytes(数据); //或任何其他TEncoding你需要
LEN:=长度(ARR);
结果:= TJavaArray&LT;&字节GT; .Create(LEN);
为X:= 0到LEN-1做
Result.Items [X]:=编曲[X];
结束;另外,摆脱循环的:
函数StringToJA(数据:字符串):TJavaArray&LT;字节取代;
VAR
编曲:TB的;
LEN:整数;
开始
编曲:= TEncoding.Default.GetBytes(数据);
LEN:=长度(ARR);
结果:= TJavaArray&LT;&字节GT; .Create(LEN);
如果len&GT; 0,则移动(编曲[0],Result.Data ^,LEN);
结束;
我怎样才能将这些行?
txt.getBytes(codeL);
sendText [2] =(字节)一个;
块引用>此外,通过利用
TEncoding
类,例如:函数StringToJA(数据:字符串;字符集:字符串=''):TJavaArray&LT;字节取代;
VAR
编码:TEncoding;
编曲:TB的;
LEN:整数;
开始
如果字符集&LT;&GT; '' 然后
编码:= TEncoding.GetEncoding(字符集)
其他
编码:= TEncoding.Default;
编曲:= Encoding.GetBytes(数据);
LEN:=长度(ARR);
结果:= TJavaArray&LT;&字节GT; .Create(LEN);
如果len&GT; 0,则移动(编曲[0],Result.Data ^,LEN);
结束;
VAR
TXT:字符串;
数据:TJavaArray&LT;字节取代;
sendText:TJavaArray&LT;字节取代;
开始
TXT:='SOME TEXT ... SCZ';
数据:= StringToJA(TXT,'ISO8859-2'); sendText:= TJavaArray&LT;&字节GT; .Create(3);
sendText.Items [0] = $ 1B;
sendText.Items [1]:= $ 74条;
sendText.Items [2]:=字节(23); ostream.write(sendText);
ostream.write(数据);I am working on Android Bluetooth Printing. Printer is standard POS thermal printer.
This is my way to send data to printer (THIS PART IS WORKING):
var Adapter: JBluetoothAdapter; remoteDevice: JBluetoothDevice; Sock: JBluetoothSocket; ostream: JOutputStream; begin Adapter := TJBluetoothAdapter.JavaClass.getDefaultAdapter; remoteDevice := Adapter.getRemoteDevice(stringtojstring(targetMAC)); Sock := remoteDevice.createRfcommSocketToServiceRecord(uid); ostream := Sock.getOutputStream; .... function StringToJA(Data: String): TJavaArray<Byte>; var X: integer; len: integer; begin len := Length(Data); Result := TJavaArray<Byte>.Create(len); for X := 0 to len do begin Result.Items[X] := Ord(Data[X]); end; end; // THIS LINE PRINT TEXT ostream.write(StringToJA(txt));
Now I want to load different char encoing and this is code in JAVA:
int a=23; String codeL="iso8859-2"; String txt="SOME TEXT...ščž"; byte data[] = txt.getBytes(codeL); byte sendText[]=new byte[3]; sendText[0]=0x1B; sendText[1]=0x74; sendText[2]=(byte)a; mobileprint.PRTSendBuffer(sendText, sendText.length);
How can I convert these lines?
- txt.getBytes(codeL);
- sendText[2]=(byte)a;
Delphi XE5 code :
... A : Integer; txt, codeL: string; data : TJavaArray<Byte>; sendText : TJavaArray<Byte>; begin A := 23; codeL := 'iso8859-2'; txt := 'SOME TEXT...ščž'; data := ??? txt.getBytes(codeL); ??? sendText := TJavaArray<Byte>.Create(3); sendText.Items[0] := $1B; sendText.Items[1] := $74; sendText.Items[2] := ???Byte(A)???; ostream.write(sendText); ostream.write(data);
This are JAVA Expressions values from Exlipse:
解决方案for X := 0 to len do
That should be using
len-1
, otherwise you will go out of bounds of both the array and the string.You are also not taking Unicode into account when copying the string to the byte array, so you are likely to run into problems if the
string
contains any non-ASCII characters that require the use of UTF-16 surrogates. You should use theSysUtils.TEncoding
class to convert thestring
to a Delphi byte array first, and then copy that into the Java byte array, eg:function StringToJA(Data: String): TJavaArray<Byte>; var Arr: TBytes; len, X: integer; begin Arr := TEncoding.Default.GetBytes(Data); // or any other TEncoding you need len := Length(Arr); Result := TJavaArray<Byte>.Create(len); for X := 0 to len-1 do Result.Items[X] := Arr[X]; end;
Alternatively, get rid of the loop:
function StringToJA(Data: String): TJavaArray<Byte>; var Arr: TBytes; len: integer; begin Arr := TEncoding.Default.GetBytes(Data); len := Length(Arr); Result := TJavaArray<Byte>.Create(len); if len > 0 then Move(Arr[0], Result.Data^, len); end;
How can I convert these lines?
txt.getBytes(codeL); sendText[2]=(byte)a;
Again, by making use of the
TEncoding
class, eg:function StringToJA(Data: string; charset: String = ''): TJavaArray<Byte>; var Encoding: TEncoding; Arr: TBytes; len: integer; begin if charset <> '' then Encoding := TEncoding.GetEncoding(charset) else Encoding := TEncoding.Default; Arr := Encoding.GetBytes(Data); len := Length(Arr); Result := TJavaArray<Byte>.Create(len); if len > 0 then Move(Arr[0], Result.Data^, len); end;
var txt: string; data : TJavaArray<Byte>; sendText : TJavaArray<Byte>; begin txt := 'SOME TEXT...ščž'; data := StringToJA(txt, 'iso8859-2'); sendText := TJavaArray<Byte>.Create(3); sendText.Items[0] := $1B; sendText.Items[1] := $74; sendText.Items[2] := Byte(23); ostream.write(sendText); ostream.write(data);
这篇关于Java字节数组德尔福字节ARRY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!