Voilà,
un int est codé sur 32 bits. Je souhaiterais accéder directement à ces bits pour faire la conversion entre un entier et les 4 bytes correspondant. Parce que pour le moment je refais le calcul.
Merci d'avance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class ByteBufferConsol { private byte[] b; private int index; private final static int B3 = (int) Math.pow(256, 3); private final static int B2 = (int) Math.pow(256, 2); private final static int B1 = 256; public ByteBufferConsol() { b = new byte[1000]; index = 0; } public ByteBufferConsol(int capacity) { b = new byte[capacity]; index = 0; } public void putInt(int i) { int res, j; res = i; j = res / B3; res = res - j; b[index++] = (byte) j; j = res / B2; res = res - j; b[index++] = (byte) j; j = res / B1; res = res - j; b[index++] = (byte) j; b[index++] = (byte) res; } public int getInt() { int result; result = b[index++] * B3 + b[index++] * B2 + b[index++] * B1 + b[index++]; return (result); } public void putInt(int i, int offset) { index = offset; putInt(i); } public int getInt(int offset) { index = offset; return getInt(); } }
Partager