Vraag komt eigenlijk neer op 1 simpel dingetje..
[UPDATE]
Ok wat hier onder staat is opgelost, nu zit ik dus met het probleem dat het ook andersom moet werken.. het gaat om J2ME hier niet om J2SE, dus new String(bytes[], "UTF8") en dergelijk werken niet!!

Ik heb een char[] die ik wil converteren naar UTF-8, daar heb ik bij SUN dit stukje code voor gevonden:
Ze willen dus een "an array of Unicode scalar values (code points)"
Hoe converteer een char naar een Unicode scalar value in een int..
Ik vermoed gewoon iets van
Weet iemand of dit de correcte manier is of hoe het wel moet?
[UPDATE]
Ok wat hier onder staat is opgelost, nu zit ik dus met het probleem dat het ook andersom moet werken.. het gaat om J2ME hier niet om J2SE, dus new String(bytes[], "UTF8") en dergelijk werken niet!!
Ik heb een char[] die ik wil converteren naar UTF-8, daar heb ik bij SUN dit stukje code voor gevonden:
code:
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
50
51
| /**
* Converts an array of Unicode scalar values (code points) into
* UTF-8. This algorithm works under the assumption that all
* surrogate pairs have already been converted into scalar code
* point values within the argument.
*
* @param ch an array of Unicode scalar values (code points)
* @returns a byte[] containing the UTF-8 encoded characters
*/
public static byte[] encode(int[] ch) {
// determine how many bytes are needed for the complete conversion
int bytesNeeded = 0;
for (int i=0; i<ch.length; i++) {
if (ch[i] < 0x80) {
++bytesNeeded;
}
else if (ch[i] < 0x0800) {
bytesNeeded += 2;
}
else if (ch[i] < 0x10000) {
bytesNeeded += 3;
}
else {
bytesNeeded += 4;
}
}
// allocate a byte[] of the necessary size
byte[] utf8 = new byte[bytesNeeded];
// do the conversion from character code points to utf-8
for(int i=0, bytes = 0; i<ch.length; i++) {
if(ch[i] < 0x80) {
utf8[bytes++] = (byte)ch[i];
}
else if (ch[i] < 0x0800) {
utf8[bytes++] = (byte)(ch[i] >> 6 | 0xC0);
utf8[bytes++] = (byte)(ch[i] & 0x3F | 0x80);
}
else if (ch[i] < 0x10000) {
utf8[bytes++] = (byte)(ch[i] >> 12 | 0xE0);
utf8[bytes++] = (byte)(ch[i] >> 6 & 0x3F | 0x80);
utf8[bytes++] = (byte)(ch[i] & 0x3F | 0x80);
}
else {
utf8[bytes++] = (byte)(ch[i] >> 18 | 0xF0);
utf8[bytes++] = (byte)(ch[i] >> 12 & 0x3F | 0x80);
utf8[bytes++] = (byte)(ch[i] >> 6 & 0x3F | 0x80);
utf8[bytes++] = (byte)(ch[i] & 0x3F | 0x80);
}
}
return utf8;
} |
Ze willen dus een "an array of Unicode scalar values (code points)"
Hoe converteer een char naar een Unicode scalar value in een int..
Ik vermoed gewoon iets van
code:
1
2
| char c = 'a'; int i = (int) c; |
Weet iemand of dit de correcte manier is of hoe het wel moet?
Opera OpenOffice.org Jabber Psi jabber://llama@mordax.com