随机24字节Byte Array通过base64编码获得32字节字符串

Deep_Learning 2014-05-26 04:46:20
1、java怎么随机生成24字节Byte Array(不能有重复)
2、然后Array通过base64编码获得32字节字符串
...全文
1265 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2014-06-04
  • 打赏
  • 举报
回复
楼主,你给的分数,我感觉心里不太平衡。 楼主的要求,我已经编码实现了,并且,在后续的过程中做了详细的解释。 但是,3楼的代码没有达到楼主的要求,却给了10分。后续的一个问题(并不是楼主所要的算法)却给了20分。 那我以后岂不是随便搞错点什么东西,就能拿得比那些认真解决问题的人拿得多了? 如果有人比我的算法好,还能实现楼主的要求,那我无话可说。 但是,3楼的算法,虽然保证了24个字节的每个字节各不相同,但是,无法保证这个序列的不可重复性。 楼主的要求,是要取得像UUID那样的不能重复的数据。
可乐罐 2014-06-04
  • 打赏
  • 举报
回复
这楼主给分属于:肥水不流外人田
冰思雨 2014-05-28
  • 打赏
  • 举报
回复
思路很简单,你不是要24字节的随机数嘛 ,我就凑24字节就好了。 首先,long型数据占8字节,那么,弄3个这样的长整形数据拼凑一下就OK了。 我的方法是将系统时间、伪随机数、自增序列这三个long型数据拼凑在一起。 系统时间,精确到毫秒级,连续获取时有可能存在重复的情况, 所以,将伪随机数拼凑进去,相同种子产生的伪随机数序列,重复的可能性更低一些, 要想去除重复,那么,拼凑一个自增序列进去就可以了,每次获取时该long型数据都会自动增加,变成更大一点的值。 拼凑完成后,将3个long型数据,转换成字节数组,长度正好24字节。 最后,进行Base64编码。
冰思雨 2014-05-28
  • 打赏
  • 举报
回复
将long型的数据,填充到buffer中。long型数据占8字节,所以,要从缓冲区指定的位置(offset)开始向后填充8字节的数据。
vnvlyp 2014-05-27
  • 打赏
  • 举报
回复
引用 4 楼 u010719640 的回复:
[quote=引用 3 楼 vnvlyp 的回复:]
	public static void main(String[] args) {
		byte[] bytes = getRandomByteArray(24);
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println(encoder.encode(bytes));
	}
	
	public static byte[] getRandomByteArray(int len) {
		if (len < 0 || len > 256) {
			throw new IllegalArgumentException("Illegal length: " + len);
		}
		
		byte[] bytes = new byte[256];
		for (int i = 0; i < 256; i++) {
			bytes[i] = (byte)i;
		}
		
		Random rand = new Random();
		byte[] randBytes = new byte[len];
		int size = 256;
		while (--len >= 0) {
			int index = rand.nextInt(size);
			randBytes[len] = bytes[index];
			bytes[index] = bytes[--size];
		}
		
		return randBytes;
	}
getRandomByteArray这个方法能保证得到的数组是唯一的嘛,比如我生成1000w个[/quote] 难道我理解错了?你的意思是像我代码保证的数组24个字节各不相同, 还是你生成的1000W个数组之间各不相同?
冰思雨 2014-05-27
  • 打赏
  • 举报
回复
import java.util.concurrent.atomic.AtomicLong;

import org.apache.catalina.util.Base64;

public class Random {

	private static final AtomicLong sequence = new AtomicLong();
	private static void convert(long data, byte[] buffer, int offset){
		buffer[offset+0] = (byte)((data>>>56)&0xFF);
		buffer[offset+1] = (byte)((data>>>48)&0xFF);
		buffer[offset+2] = (byte)((data>>>40)&0xFF);
		buffer[offset+3] = (byte)((data>>>32)&0xFF);
		buffer[offset+4] = (byte)((data>>>24)&0xFF);
		buffer[offset+5] = (byte)((data>>>16)&0xFF);
		buffer[offset+6] = (byte)((data>>> 8)&0xFF);
		buffer[offset+7] = (byte)((data>>> 0)&0xFF);
	}
	//该方法在多线程编程中要注意用法(单例),否则会产生重复数据。
	public String next(){
		long[] rands = new long[3];
		rands[1] = System.currentTimeMillis();
		java.util.Random jRand = new java.util.Random(rands[1]);
		rands[0] = jRand.nextLong();
		rands[2] = sequence.incrementAndGet();
		byte[] data = new byte[24];
		convert(rands[0], data,  0);
		convert(rands[1], data,  8);
		convert(rands[2], data, 16);
		return Base64.encode(data);
	}
	
	public static void main(String[] args) {
		Random rand = new Random();
		System.out.println(rand.next());
		System.out.println(rand.next());
	}

}
Deep_Learning 2014-05-27
  • 打赏
  • 举报
回复
引用 2 楼 preferme 的回复:
截取UUID的数据就可以了。
客户说UUID不行,我就无语了
Deep_Learning 2014-05-27
  • 打赏
  • 举报
回复
引用 3 楼 vnvlyp 的回复:
	public static void main(String[] args) {
		byte[] bytes = getRandomByteArray(24);
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println(encoder.encode(bytes));
	}
	
	public static byte[] getRandomByteArray(int len) {
		if (len < 0 || len > 256) {
			throw new IllegalArgumentException("Illegal length: " + len);
		}
		
		byte[] bytes = new byte[256];
		for (int i = 0; i < 256; i++) {
			bytes[i] = (byte)i;
		}
		
		Random rand = new Random();
		byte[] randBytes = new byte[len];
		int size = 256;
		while (--len >= 0) {
			int index = rand.nextInt(size);
			randBytes[len] = bytes[index];
			bytes[index] = bytes[--size];
		}
		
		return randBytes;
	}
getRandomByteArray这个方法能保证得到的数组是唯一的嘛,比如我生成1000w个
Deep_Learning 2014-05-27
  • 打赏
  • 举报
回复
引用 6 楼 preferme 的回复:
import java.util.concurrent.atomic.AtomicLong;

import org.apache.catalina.util.Base64;

public class Random {

	private static final AtomicLong sequence = new AtomicLong();
	private static void convert(long data, byte[] buffer, int offset){
		buffer[offset+0] = (byte)((data>>>56)&0xFF);
		buffer[offset+1] = (byte)((data>>>48)&0xFF);
		buffer[offset+2] = (byte)((data>>>40)&0xFF);
		buffer[offset+3] = (byte)((data>>>32)&0xFF);
		buffer[offset+4] = (byte)((data>>>24)&0xFF);
		buffer[offset+5] = (byte)((data>>>16)&0xFF);
		buffer[offset+6] = (byte)((data>>> 8)&0xFF);
		buffer[offset+7] = (byte)((data>>> 0)&0xFF);
	}
	//该方法在多线程编程中要注意用法(单例),否则会产生重复数据。
	public String next(){
		long[] rands = new long[3];
		rands[1] = System.currentTimeMillis();
		java.util.Random jRand = new java.util.Random(rands[1]);
		rands[0] = jRand.nextLong();
		rands[2] = sequence.incrementAndGet();
		byte[] data = new byte[24];
		convert(rands[0], data,  0);
		convert(rands[1], data,  8);
		convert(rands[2], data, 16);
		return Base64.encode(data);
	}
	
	public static void main(String[] args) {
		Random rand = new Random();
		System.out.println(rand.next());
		System.out.println(rand.next());
	}

}
请问
convert(long data, byte[] buffer, int offset)
,这个方法是做什么的?
Deep_Learning 2014-05-27
  • 打赏
  • 举报
回复
引用 7 楼 vnvlyp 的回复:
[quote=引用 4 楼 u010719640 的回复:] [quote=引用 3 楼 vnvlyp 的回复:]
	public static void main(String[] args) {
		byte[] bytes = getRandomByteArray(24);
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println(encoder.encode(bytes));
	}
	
	public static byte[] getRandomByteArray(int len) {
		if (len < 0 || len > 256) {
			throw new IllegalArgumentException("Illegal length: " + len);
		}
		
		byte[] bytes = new byte[256];
		for (int i = 0; i < 256; i++) {
			bytes[i] = (byte)i;
		}
		
		Random rand = new Random();
		byte[] randBytes = new byte[len];
		int size = 256;
		while (--len >= 0) {
			int index = rand.nextInt(size);
			randBytes[len] = bytes[index];
			bytes[index] = bytes[--size];
		}
		
		return randBytes;
	}
getRandomByteArray这个方法能保证得到的数组是唯一的嘛,比如我生成1000w个[/quote] 难道我理解错了?你的意思是像我代码保证的数组24个字节各不相同, 还是你生成的1000W个数组之间各不相同?[/quote] 1000w个数组各不相同
vnvlyp 2014-05-26
  • 打赏
  • 举报
回复
	public static void main(String[] args) {
		byte[] bytes = getRandomByteArray(24);
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println(encoder.encode(bytes));
	}
	
	public static byte[] getRandomByteArray(int len) {
		if (len < 0 || len > 256) {
			throw new IllegalArgumentException("Illegal length: " + len);
		}
		
		byte[] bytes = new byte[256];
		for (int i = 0; i < 256; i++) {
			bytes[i] = (byte)i;
		}
		
		Random rand = new Random();
		byte[] randBytes = new byte[len];
		int size = 256;
		while (--len >= 0) {
			int index = rand.nextInt(size);
			randBytes[len] = bytes[index];
			bytes[index] = bytes[--size];
		}
		
		return randBytes;
	}
冰思雨 2014-05-26
  • 打赏
  • 举报
回复
截取UUID的数据就可以了。
可乐罐 2014-05-26
  • 打赏
  • 举报
回复
楼主想问的是啥?

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧