0

I get to be a tool bluetooth number of 16-bit and I need to convert it to a string. I am able to obtain a number up to 255 in this way:

byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf);                              
char c = strIncom.charAt(0);

definitely receive up to 255 because "byte" it is 8-bit. I tried to replace bytes with short but I get this error:

byte[] cannot be cast to short[]
davix10
  • 51
  • 1
  • 8
  • Do you need to convert byte[] to short[]? – nogard Sep 01 '14 at 08:38
  • I would like to get the numbers greater than 255 but do not know how to do @nogard – davix10 Sep 01 '14 at 08:41
  • @davix10 : Not an answer to your cast problem (you haven't really shown enough code) but do you know that a `byte` in Java is signed so it can only represent +/-127? Or are you manipulating directly with a bit-mask or bit-wise operators? – Squonk Sep 01 '14 at 08:47
  • Still unclear of what you want to do, but perhaps your issue is more related to the string encoding than raw byte value? – Andrew T. Sep 01 '14 at 08:52
  • @Squonk At this time I can get the numbers up to 255, I know that byte is 8-bit, but do not know how to solve the problem. You need more details? – davix10 Sep 01 '14 at 08:57
  • @AndrewT. Now do you understand? – davix10 Sep 01 '14 at 09:15
  • @davix10 what do you mean by 'get numbers up to 255'? where do you 'get' them from? – Gumbo Sep 01 '14 at 09:20
  • Barely. Do you want to convert it to `short[]` instead? If it is, then maybe [this question](http://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java) could help. But take note that `String` doesn't have constructor with `short[]` parameter. – Andrew T. Sep 01 '14 at 09:21
  • @AndrewT. but if I convert byte in short do not always get the numbers up to 255? – davix10 Sep 01 '14 at 09:27

1 Answers1

0

try:

  ByteBuffer input=ByteBuffer.wrap((byte[])msg.obj);
  short my16bitValue=input.getShort();

read about byte order

I'm not sure, whether that is what you want to do...

(you can get a String from that using Short.toString(my16bitValue) ...)

DThought
  • 1,340
  • 7
  • 18