how can i convert byte to JByte in delphi xe5?

1.3k Views Asked by At

have jstringtostring or stringtojstring. how can i convert byte to JByte (or other variables types)?

....
var x:jbyte;
begin
 x:=bytetojbyte(65);
....
end;

thanks.

2

There are 2 best solutions below

1
On BEST ANSWER

java.io.OutputStream.write() expects a byte[] (ie, an array of bytes) as input. Delphi's JByte interface is a JNI wrapper for the java.lang.Byte class, which is itself a Java object wrapper for a single byte value. JByte does not represent an array of bytes, so you cannot use JByte with OutputStream.write().

If you look at the declaration of Delphi's JOutputStream interface (the JNI wrapper for java.io.OutputStream) in the Androidapi.JNI.JavaTypes unit, it has write() methods that expect TJavaArray<Byte> as input. TJavaArray<T> is declared in the Androidapi.JNIBridge unit. If you are trying to pass a data buffer from your Delphi code to OutputStream.write(), you will have to declare a TJavaArray<Byte> variable, allocate it to the desired length, and copy your data into it, eg:

uses
  ..., Androidapi.JNIBridge, Androidapi.JNI.JavaTypes;

var
  buffer: TJavaArray<Byte>;
begin
  buffer := TJavaArray<Byte>.Create(DesiredLength);
  Move(SourceByteData^, buffer.Data^, DesiredLength);
  SomeOutputStream.write(buffer);
end;
3
On
function stringtoJbyte(metin:string):TJavaArray<Byte>;
var
  buffer: TJavaArray<Byte>;
  i:integer;
begin
  buffer := TJavaArray<Byte>.Create(length(metin)+1);
  for i := 1 to Length(metin) do
        buffer.Items[i]:= ord( (metin[i-1]) );
  result:=buffer;
end;

I use this code! it work for me. thanks again remy lebeau..

encoded (for ı ş İ ç ö ğ ...) function;

function stringtoJbyte(metin:String):TJavaArray<byte>;
var
  buffer: TJavaArray<byte>;
  bufferByte:TArray<byte>;
  i,len:integer;
begin
  bufferByte:=TEncoding.UTF8.GetBytes(metin);
  //len:=ByteLength(metin);
  len:=length(bufferByte);
  buffer := TJavaArray<byte>.Create(len+1);
  for i := 0 to len do
        buffer.Items[i+1]:=bufferByte[i] ;
  result:=buffer;
end;