长到 base64 字符的数字
我正在处理一个需要一些 Base64 编码的小任务。我试图在头脑中做到这一点,但迷路了。
我有一个java长格式的13位数字,比如: 1294705313608 、 1294705313594 、 1294705313573
我用它做了一些处理,基本上我把这个数字附加在东西后面,把它放在一个字节数组中,然后使用以下方法将其转换为base64:
String b64String = new sun.misc.BASE64Encoder().encodeBuffer(bArray);
现在,我我知道对于我的原始号码,前 3 位数字永远不会改变。所以 129 在上面的数字中是常数。我想知道有多少对应于这些数字的字符在生成的 base64 字符串中不会改变。
将 long 序列化为字节数组的代码。我忽略前 2 个字节,因为它们始终为 0:
bArray[0] = (byte) (time >>> 40);
bArray[1] = (字节) (时间>>>32);
bArray[2] = (字节) (时间>>24);
bArray[3] = (字节) (时间>>16);
bArray[4] = (字节) (时间>>>8);
bArray[5] = (字节) (时间>>>0);
谢谢。
笔记: 我知道 Base64 会占用 6 位并从中生成一个字符。因此,如果前 3 位数字在 long 中不发生变化,那么在 Base64 中有多少个字符不会发生变化。 这不是硬件作业,但我对编码不太熟悉。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1290000000000 是二进制的
10010110001011001111111011110010000000000
。1299999999999 是二进制的
10010111010101110000010011100011111111111
。两者都是 41 位长,前 7 位之后有所不同。您的移位将第 41-48 位放在第一个字节中,该字节始终为
00000001
。接下来的字节将始终是00101110
、00101101
或00101110
。因此,您在所有可能的数组值中都有共同的前导 14 位,这(每个编码的 Base64 字符有 6 位)意味着编码字符串中有 2 个共同字符。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 be00101110
,00101101
, or00101110
. 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.看来你走在正确的轨道上。我认为你想要做的是将 long 转换为字节数组,然后将字节数组转换为 Base64。
如何将 Long 转换为 byte[] 和返回 java 向您展示如何将其转换为字节。
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.