1

I'm currently stuck trying to convert a Java.Nio.ByteBuffer to an array or a string in a Xamarin Android App.

byte[] outputBytes = new byte[16];
Java.Nio.Buffer outputBuffer = ByteBuffer.Wrap(outputBytes);
//... filling buffer (using tflite model)
byte[] bufferBytes = ???

How can I get the content of the buffer?

Apparently there is a function like buffer.array(), but it doesn't work here.

So if anyone has an idea how i can make this work or any other way, help would be really appreciated.

mezzomixx
  • 11
  • 1
  • possible duplicate of https://stackoverflow.com/q/17354891 or https://stackoverflow.com/q/1252468 – hongsy Jan 27 '20 at 16:20
  • Not a duplicate. Suggestions in first link don't work due to working in another environment (I guess), which is exactly the problem here. – mezzomixx Jan 27 '20 at 16:25
  • Second one too, I'm not writing plain java code, I can't access the methods mentioned. – mezzomixx Jan 27 '20 at 16:27
  • what about https://stackoverflow.com/q/43117553 or https://forums.xamarin.com/discussion/28763/efficient-conversion-of-bytearray-to-byte? – hongsy Jan 27 '20 at 16:30
  • The first one actually looks like it might help, but I'm getting this with every method I tried: Error CS1061 "Buffer" does not contain a definition for "GetFloat", and no accessible GetFloat extension method that accepts a first argument of type "Buffer" could be found (a using directive or assembly reference may be missing). – mezzomixx Jan 27 '20 at 16:41
  • ByteBuffer provides a method of GetFloat(Int32), have you tried that? https://learn.microsoft.com/en-us/dotnet/api/Java.Nio.ByteBuffer?view=xamarin-android-sdk-9 – Wendy Zang - MSFT Jan 28 '20 at 06:35
  • Yes, I tried, but it gives me the error shown above. `"Buffer" does not contain a definition for "GetFloat"` – mezzomixx Jan 28 '20 at 10:02
  • Change Java.Nio.Buffer to var, then you could use GetFloat method. `byte[] outputBytes = new byte[16]; var outputBuffer = ByteBuffer.Wrap(outputBytes); //... filling buffer (using tflite model) var bufferBytes = outputBuffer.GetFloat(2);` – Wendy Zang - MSFT Feb 03 '20 at 08:42

0 Answers0