Guest User

Untitled

a guest
Jan 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. package com.mango.utils;
  2.  
  3. public class WiredEncoding
  4. {
  5. public static byte NEGATIVE = 72;
  6. public static byte POSITIVE = 73;
  7. public static int MAX_INTEGER_BYTE_AMOUNT = 6;
  8.  
  9. public static byte[] EncodeInt32(int i)
  10. {
  11. byte[] wf = new byte[WiredEncoding.MAX_INTEGER_BYTE_AMOUNT];
  12.  
  13. int pos = 0;
  14. int numBytes = 1;
  15. int startPos = pos;
  16. int negativeMask = i >= 0 ? 0 : 4;
  17.  
  18. i = Math.abs(i);
  19.  
  20. wf[pos++] = (byte)(64 + (i & 3));
  21.  
  22. for (i >>= 2; i != 0; i >>= WiredEncoding.MAX_INTEGER_BYTE_AMOUNT)
  23. {
  24. numBytes++;
  25. wf[pos++] = (byte)(64 + (i & 0x3f));
  26. }
  27. wf[startPos] = (byte)(wf[startPos] | numBytes << 3 | negativeMask);
  28.  
  29. byte[] bzData = new byte[numBytes];
  30.  
  31. for (int x = 0; x < numBytes; x++)
  32. {
  33. bzData[x] = wf[x];
  34. }
  35.  
  36. return bzData;
  37. }
  38.  
  39. public static int DecodeInt32(byte[] bzData, int totalBytes)
  40. {
  41. int pos = 0;
  42. int v = 0;
  43.  
  44. boolean negative = (bzData[pos] & 4) == 4;
  45.  
  46. totalBytes = bzData[pos] >> 3 & 7;
  47. v = bzData[pos] & 3;
  48.  
  49. pos++;
  50.  
  51. int shiftAmount = 2;
  52.  
  53. for (int b = 1; b < totalBytes; b++)
  54. {
  55. v |= (bzData[pos] & 0x3f) << shiftAmount;
  56. shiftAmount = 2 + 6 * b;
  57. pos++;
  58. }
  59.  
  60. if (negative == true)
  61. {
  62. v *= -1;
  63. }
  64.  
  65. return v;
  66. }
  67. }
Add Comment
Please, Sign In to add comment