Skip to content

Instantly share code, notes, and snippets.

@Romain-P
Created October 19, 2017 09:31
Show Gist options
  • Select an option

  • Save Romain-P/92df896f5f08ed2bd4b592b122e1ad18 to your computer and use it in GitHub Desktop.

Select an option

Save Romain-P/92df896f5f08ed2bd4b592b122e1ad18 to your computer and use it in GitHub Desktop.
package com.company;
public class Main {
public static void main(String[] args) {
System.out.println(read_vi32(write_vi32(10)));
}
static byte[] write_vi32(int res) {
int i = 0;
byte[] bytes = new byte[4];
do {
int res1 = res & 0b01111111;
res = res >> 7;
bytes[i] = (byte) (res1 | (res != 0 ? 0b10000000 : 0));
i++;
} while (i < 4 && res != 0);
return bytes;
}
static int read_vi32(byte[] bytes) {
int res = 0;
for (int i = 0; i < 4; i++) {
byte b = bytes[i];
if ((b & 0b10000000) != 0) {
break;
}
res = (res << 7) | (b & 0b01111111);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment