How to initialiaze a unsigned byte array with a set of well known values in Java 7?

只谈情不闲聊 提交于 2019-12-11 04:19:09

问题


I want to initialize a byte array with unsigned byte values ranging from 0 to 255.

Here is how I did it:

int N = 256;
char[] tmp = new char[N];

for (char c = 0; c < N; c++) {
    tmp[c] = c;
}

byte[] table = new byte[N];
table = Charset.forName("US-ASCII").encode(CharBuffer.wrap(tmp)).array();

int i=1;
for(byte b : table) {
    System.out.format("%02X ",  b);
    if(i%10==0) {
        System.out.println();
    }
    i++;
}

This produces:

00 01 02 03 04 05 06 07 08 09 
0A 0B 0C 0D 0E 0F 10 11 12 13 
14 15 16 17 18 19 1A 1B 1C 1D 
1E 1F 20 21 22 23 24 25 26 27 
28 29 2A 2B 2C 2D 2E 2F 30 31 
32 33 34 35 36 37 38 39 3A 3B 
3C 3D 3E 3F 40 41 42 43 44 45 
46 47 48 49 4A 4B 4C 4D 4E 4F 
50 51 52 53 54 55 56 57 58 59 
5A 5B 5C 5D 5E 5F 60 61 62 63 
64 65 66 67 68 69 6A 6B 6C 6D 
6E 6F 70 71 72 73 74 75 76 77 
78 79 7A 7B 7C 7D 7E 7F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F 
3F 3F 3F 3F 3F 3F 3F 3F 3F 3F

Why is there a bunch of 3F ? Why can't I get 80, 81 ... after 7F ?

EDIT: Here is how I solve my issue

int N = 256;
byte[] table = new byte[N];

for (int i = 0; i < N; i++) {
    table[i] = (byte) i;
}

int i=1;
for(byte b : table) {
    System.out.format("%02X ",  b);
    if(i%10==0) {
        System.out.println();
    }
    i++;
}

Outputs

00 01 02 03 04 05 06 07 08 09 
0A 0B 0C 0D 0E 0F 10 11 12 13 
14 15 16 17 18 19 1A 1B 1C 1D 
1E 1F 20 21 22 23 24 25 26 27 
28 29 2A 2B 2C 2D 2E 2F 30 31 
32 33 34 35 36 37 38 39 3A 3B 
3C 3D 3E 3F 40 41 42 43 44 45 
46 47 48 49 4A 4B 4C 4D 4E 4F 
50 51 52 53 54 55 56 57 58 59 
5A 5B 5C 5D 5E 5F 60 61 62 63 
64 65 66 67 68 69 6A 6B 6C 6D 
6E 6F 70 71 72 73 74 75 76 77 
78 79 7A 7B 7C 7D 7E 7F 80 81 
82 83 84 85 86 87 88 89 8A 8B 
8C 8D 8E 8F 90 91 92 93 94 95 
96 97 98 99 9A 9B 9C 9D 9E 9F 
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 
AA AB AC AD AE AF B0 B1 B2 B3 
B4 B5 B6 B7 B8 B9 BA BB BC BD 
BE BF C0 C1 C2 C3 C4 C5 C6 C7 
C8 C9 CA CB CC CD CE CF D0 D1 
D2 D3 D4 D5 D6 D7 D8 D9 DA DB 
DC DD DE DF E0 E1 E2 E3 E4 E5 
E6 E7 E8 E9 EA EB EC ED EE EF 
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 
FA FB FC FD FE FF

回答1:


Why is there a bunch of 3F ? Why can't I get 80, 81 ... after 7F ?

Because your character array contains non-ASCII characters. Charset.encode is encoding those as 3F (ASCII for '?') to represent the fact that they're not in ASCII.

From the documentation:

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string.

It looks like you don't really want text data to start with, so I suggest you avoid it completely. Just populate the byte array directly:

byte[] table = new byte[N];
for (int i = 0; i < N; i++) {
    table[i] = (byte) i;
}

Note that Java doesn't really have unsigned bytes - the above will end up with negative values for indexes 128-255... but they'll have the right bits set. You can treat them as unsigned values by masking them with 0xff:

int x = table[200] & 0xff; // Now x is 200



回答2:


The closest encoding is ISO-8859-1, it is doesn't appear you want encoding at all.

You can copy the characters one at a time

char[] tmp = new char[256];

for(char c = 0; c < tmp.length; c++)
    tmp[c] = c;

byte[] table = new byte[tmp.length];
for(int i = 0; i < tmp.length; i++) {
    table[i] = (byte) tmp[i];

    System.out.format("%02X ",  table[i] & 0xff);
    if(i % 16 == 15) {
        System.out.println();
    }
}

prints

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 
50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 
60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 
70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 
80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 
90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F 
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF 
B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF 
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF 
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF 
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF 
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 



回答3:


    int N = 256;

    byte[] table = new byte[N];
    for(int b = 1; b<N; b++){
        table[b] = (byte) b;
    }


来源:https://stackoverflow.com/questions/20859793/how-to-initialiaze-a-unsigned-byte-array-with-a-set-of-well-known-values-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!