Libraryless. Click here for Pure Java version (7989L/45K).
1 | scope byteArrayToBase128Utf8 |
2 | |
3 | static int #byteFromArray(byte[] data, int i) { |
4 | ret i >= data.length ? 0 : ubyteToInt(data[i]); |
5 | } |
6 | |
7 | static byte[] byteArrayToBase128Utf8(byte[] data) { |
8 | int n = l(data); |
9 | bool anomaly = (n%7) == 0; |
10 | int nOut = toInt((n*8L+6)/7); |
11 | byte[] out = new byte[anomaly ? nOut+1 : nOut]; |
12 | int iIn = 0, iOut = 0; |
13 | while (iOut < nOut) { |
14 | // take top 7 bits of byte 0 |
15 | out[iOut] = (byte) (byteFromArray(data, iIn) >> (8-7)); |
16 | if (++iOut >= nOut) break; |
17 | // take bottom bit of byte 0 + top 6 bits of byte 1 |
18 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn) << (7-1) | byteFromArray(data, iIn+1) >> (8-6)); |
19 | if (++iOut >= nOut) break; |
20 | // take bottom 2 bits of byte 1 + top 5 bits of byte 2 |
21 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+1) << (7-2) | byteFromArray(data, iIn+2) >> (8-5)); |
22 | if (++iOut >= nOut) break; |
23 | // take bottom 3 bits of byte 2 + top 4 bits of byte 3 |
24 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+2) << (7-3) | byteFromArray(data, iIn+3) >> (8-4)); |
25 | if (++iOut >= nOut) break; |
26 | // take bottom 4 bits of byte 3 + top 3 bits of byte 4 |
27 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+3) << (7-4) | byteFromArray(data, iIn+4) >> (8-3)); |
28 | if (++iOut >= nOut) break; |
29 | // take bottom 5 bits of byte 4 + top 2 bits of byte 5 |
30 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+4) << (7-5) | byteFromArray(data, iIn+5) >> (8-2)); |
31 | if (++iOut >= nOut) break; |
32 | // take bottom 6 bits of byte 5 + top bit of byte 6 |
33 | out[iOut] = (byte) (0x7F & (byteFromArray(data, iIn+5) << (7-6) | byteFromArray(data, iIn+6) >> (8-1)); |
34 | if (++iOut >= nOut) break; |
35 | // take bottom 7 bits of byte 6 |
36 | out[iOut] = (byte) (0x7F & byteFromArray(data, iIn+6)); |
37 | if (++iOut >= nOut) break; |
38 | iIn += 7; |
39 | } |
40 | if (anomaly) |
41 | out[iOut] = (byte) 0xFF; |
42 | ret out; |
43 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035231 |
Snippet name: | byteArrayToBase128Utf8 |
Eternal ID of this version: | #1035231/11 |
Text MD5: | c4127e594ff30a4f525822176f71ef2d |
Transpilation MD5: | 9ba8499a1cb14ef234cdf6a5cd790cc3 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-18 04:02:49 |
Source code size: | 1909 bytes / 43 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 169 / 281 |
Version history: | 10 change(s) |
Referenced in: | [show references] |