相关疑难解决方法(0)

如何生成随机字母数字字符串?

我一直在寻找一种简单的 Java算法来生成伪随机字母数字字符串.在我的情况下,它将被用作一个唯一的会话/密钥标识符,它"很可能"在500K+生成过程中是唯一的(我的需求实际上并不需要更复杂的东西).

理想情况下,我可以根据我的唯一性需求指定长度.例如,生成的长度为12的字符串可能看起来像"AEYGF7K0DM1X".

java string random alphanumeric

1679
推荐指数
23
解决办法
134万
查看次数

如何将字节数组转换为字符串,反之亦然?

我必须在Android中将字节数组转换为字符串,但我的字节数组包含负值.

如果我再次将该字符串转换为字节数组,我得到的值与原始字节数组值不同.

我该怎么做才能获得正确的转换?我用来进行转换的代码如下:

// Code to convert byte arr to str:
byte[] by_original = {0,1,-2,3,-4,-5,6};
String str1 = new String(by_original);
System.out.println("str1 >> "+str1);

// Code to convert str to byte arr:
byte[] by_new = str1.getBytes();
for(int i=0;i<by_new.length;i++) 
System.out.println("by1["+i+"] >> "+str1);
Run Code Online (Sandbox Code Playgroud)

我陷入了这个问题.

java

228
推荐指数
11
解决办法
55万
查看次数

标签 统计

java ×2

alphanumeric ×1

random ×1

string ×1