android - In Java, Why do element of my byte array contain 2 bytes? -


i programming little java based android app receives c based unsigned char[] byte array bluetooth chip , streams byte array.

the fact android , bluetooth shouldn't matter though, background. but, using minimum api 8 if makes difference.

the main code is:

inputstream = socket.getinputstream(); datainputstream dis = new datainputstream(is); byte[] buffer = new byte[1024]; bytesread = dis.read(buffer, 0, 1024); 

however, when @ actual contents of buffer, see this:

[0] 15 [0xf] [^o]    [1] 15 [0xf] [^o]    [2] 0 [0x0] [^@ (nul)]   [3] -119 [137] [0x89] [^É]   [4] 2 [0x2] [^b]     [5] 6 [0x6] [^f]     [6] 26 [0x1a] [^z]   [7] -47 [209] [0xd1] [Ñ]     [8] -1 [255] [0xff] [ÿ]  [9] 104 [0x68] [h]   [10]    -1 [255] [0xff] [ÿ]  [11]    -46 [210] [0xd2] [Ò]     [12]    -1 [255] [0xff] [ÿ]  [13]    104 [0x68] [h]   [14]    -19 [237] [0xed] [í]     [15]    -128 [128] [0x80] [^À]   

the above copy eclipse expression view set show

  1. element [#]
  2. signed int value
  3. if negative, unsigned int value [this value not appear unless #2 negative]
  4. hex value
  5. ascii value

my question is, if byte array, why of hex values containing 2 bytes. @ elements 6 through 14, each of them of form 0x1a, 0x12, 0xff, etc. bytes 0 through 5 1 byte (except element 3).

i don't think issue bluetooth side because see actual code being made, , unsigned char[] array, each element 1 byte. plus, recall seeing in previous little project involved taking data streams online api.

how can ensure java array elements contain 1 byte? , matter, feel not understanding important java since perplexes me -- how can java allow byte array contain more 1 byte per element?

you're confusing bytes , hexadecimal digits.

a byte contains 8 bits. hexadecimal digit goes 0 f, , 4 bits (16 values, 16 = 2^4, 4 bits). single byte represented using 2 hexadecimal digits.

0xf equivalent 0x0f. first 4 bits 0, , last 4 bits 1.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -