-1

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

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

thanks.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
ilhan
  • 45
  • 1
  • 10
  • at procedure write(buffer:jbyte);cdecl;overload;.... for [javaSignature('java.io.OutputStream')]. – ilhan Dec 09 '13 at 01:04
  • yes i have seen. but i get "access violation at address ....." on byteX:=TJByte.JavaClass.init(65); Is there an easier way of this? – ilhan Dec 09 '13 at 01:26

2 Answers2

5

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • really thanks you! i think you are best.. my socket was wrote the first "A" char :D . ( http://stackoverflow.com/questions/20276318/make-tcp-server-socket-class-for-android-with-delphi-xe5#comment30272747_20276318 ) – ilhan Dec 09 '13 at 22:12
0
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;
ilhan
  • 45
  • 1
  • 10
  • Your code is not handling non-ASCII Unicode characters. Also, Java arrays are 0-indexed, same as Delphi strings on mobile compilers (but they are still 1-indexed on desktop compilers). You should convert the `string` to a `Byte` array via `TEncoding.GetBytes()` (or equivalent), then copy those bytes into the Java array using `Move()` instead of a manual loop. – Remy Lebeau Dec 13 '13 at 02:02
  • You need to replace `len:=ByteLength(metin)` with `len:=Length(bufferByte)` instead. And why are you allocating `+1`? Again, Java arrays are 0-indexed, not 1-indexed. `buffer := TJavaArray.Create(len); ... buffer.Items[i] := ...`. Better, just get rid of the loop altogether: `buffer := TJavaArray.Create(len); if len > 0 then Move(PByte(bufferByte)^, buffer.Data^, len);` – Remy Lebeau Dec 13 '13 at 21:46
  • worked project files here https://drive.google.com/file/d/0BxODt2IJsmR8cUQ4cVltbWFzYUk/edit?usp=sharing if you look it.. (very buggy, but read and write OK!) – ilhan Dec 14 '13 at 01:08