0

I am working on a small task which requires some base64 encoding. I am trying to do it in head but getting lost .

I have a 13 digit number in java long format say: 1294705313608 , 1294705313594 , 1294705313573

I do some processing with it, bascially I take this number append it with stuff put it in a byte array and then convert it to base64 using:

String b64String = new sun.misc.BASE64Encoder().encodeBuffer(bArray);

Now , I know that for my original number, the first 3 digits would never change. So 129 is constant in above numbers. I want to find out how many chars corresponding to those digits would not change in the resultant base64 string.

Code to serialize long to the byte array. I ignore the first 2 bytes since they are always 0:

bArray[0] = (byte) (time >>> 40);
bArray[1] = (byte) (time >>> 32);
bArray[2] = (byte) (time >>> 24);
bArray[3] = (byte) (time >>> 16);
bArray[4] = (byte) (time >>> 8);
bArray[5] = (byte) (time >>> 0);

Thanks.

Notes: I know that base64 would take 6 bits and make one character out of it. So if first 3 digits do not change in long how many chars would not change in base64. This in NOT a HW assignment, but I am not very familiar with encoding.

codeObserver
  • 6,521
  • 16
  • 76
  • 121

2 Answers2

1

1290000000000 is 10010110001011001111111011110010000000000 in binary.
1299999999999 is 10010111010101110000010011100011111111111 in binary.

Both are 41 bits long, and they differ after the first 7 bits. Your shift places bits 41-48 in the first byte, which will always be 00000001. The following byte will always be 00101110, 00101101, or 00101110. So you've got the leading 14 bits in common across all your possible array values, which (at 6 bits per encoded base64 char) means 2 characters in common in the encoded string.

dkarp
  • 14,483
  • 6
  • 58
  • 65
  • Thnx dkarpp, I am confused between "they differ after the first 7 bits" & "you've got the leading 14 bits in common across all your possible array values" ... 7 bits can make a 3 digit number and hence I can understand that first 3 digits in the long doesnt change ....So the 7 bits would correspond to only 1 base64char .. so only 1 char should not change ?? .. Can you explain once more how did you derive the 14 bits ? . thnx again..I programmed it and as you can see the long is a timestmp .. – codeObserver Jan 11 '11 at 22:28
  • so for 2 different times I got these numbers. the nos are after appendng actual byte array wth more bytes so ignore its lenght: – codeObserver Jan 11 '11 at 22:30
  • You split up those 7 bits with your serialization logic. 1 bit went into the first byte, the other 6 went into the second byte. With that 1 bit in the first byte came 7 bits of all `0`s. So you have 7 bits of your `long` that don't change *and 7 bits of leading `0` as well*. – dkarp Jan 11 '11 at 22:34
  • shift places bits 41-48 in the first byte is correct .. it will not be 00000001 .. it will be whatever the time variable has in bits 41-48. Those 7 bits go into bArray[0] .. and hence I am saying 1 base64 character – codeObserver Jan 11 '11 at 22:58
  • The time variable has `00000001` in bits 41-48. It has `00101110`, `00101101`, or `00101110` in bits 33-40. The thing you're missing is that your serialization algorithm is grabbing the first nonzero bit from the long as well as 7 bits of leading `0`. – dkarp Jan 11 '11 at 23:02
0

Appears you're on the right track. I think what you want to do is convert a long to a byte array, then convert the byte array to Base64.

How do I convert Long to byte[] and back in java shows you how to convert it to bytes.

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388