Just like the title says, I am trying to encode a string "test" into base32 string "ORSXG5A=" in Java. All I find when searching online is classes that encodes from string to array with 32bits, but obviously that is not what I want. Sorry for this newbie question. Apache commons-codec provides a Base32 class that does just that Base32 base32 = new Base32(); System.out.p
就像标题所示,我试图在Java中将字符串“test”编码为base32字符串“ORSXG5A =”。 我在网上搜索时发现的所有类都是用32位字符串编码的,但显然这不是我想要的。 对不起,这个新手问题。 Apache commons-codec提供了一个Base32类 Base32 base32 = new Base32(); System.out.println(base32.encodeAsString("test".getBytes())); 版画 ORSXG5A= 你可以在这里下载。 作为@Sotirios Delimanolis写道它可以使用Apache公用,
I'm using Eclipse. I have the following line of code: wr.write(new sun.misc.BASE64Encoder().encode(buf)); Eclipse marks this line as an error. I imported the required libraries: import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; But again, both of them are shown as errors. I found a similar post here. I used Apache Commons as the solution suggested by including: import or
我正在使用Eclipse。 我有以下代码行: wr.write(new sun.misc.BASE64Encoder().encode(buf)); Eclipse将此行标记为错误。 我导入了所需的库: import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; 但是,再一次,他们都显示为错误。 我在这里找到了类似的帖子。 我使用Apache Commons作为解决方案,其中包括: import org.apache.commons.*; 并导入从http://commons.apache.org/codec/下载的JAR文件
This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers There's a better way to get random numbers, and that's with java.util.Random . Math.random() returns a double (floating-point) value, but based on your request of a 3-digit number, I'm going to assume that what you really want is an integer. So here's what
这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 有一个更好的方法来获得随机数字,这是与java.util.Random 。 Math.random()返回一个double(浮点)值,但是根据你的一个3位数的请求,我会假设你真正想要的是一个整数。 所以这就是你所做的。 // initialize a Random object somewhere; you should only need one Random random = new Random(); // generate a random integer from 0 t
This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers private long generateRandomNumber(int n) { long min = (long) Math.pow(10, n - 1); return ThreadLocalRandom.current().nextLong(min, min * 10); } nextLong produces random numbers between lower bound inclusive and upper bound exclusive so calling it with parameters (1_
这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 private long generateRandomNumber(int n) { long min = (long) Math.pow(10, n - 1); return ThreadLocalRandom.current().nextLong(min, min * 10); } nextLong会在下限和上限之间生成随机数,因此使用参数(1_000, 10_000)调用它,例如结果数字为1000到9999.不幸的是,Old Random没有得到这些好的新功能。 但基本上没有理由继续
Possible Duplicate: Generating random number in a range with Java double x = //Random number between -0.5 and 0.5 Possible Outputs: -0.23 0.01 0.26 -0.4 How do I generate a double between the range of (example) -0.5 and 0.5 ? This should do it Math.random() - 0.5 Math.random will generate betweeen 0 and 1 . If you want between -0.5 and +0.5 then you can just -0.5 from this result. See
可能重复: 使用Java生成一个范围内的随机数 double x = //Random number between -0.5 and 0.5 可能的产出: -0.23 0.01 0.26 -0.4 如何在(例子) -0.5和0.5的范围之间生成一个双精度值? 这应该做到这一点 Math.random() - 0.5 Math.random将在0和1生成。 如果你想要在-0.5到+0.5之间,那么你可以从这个结果中得到-0.5 。 查看API文档 有一两件事,这不会做是不断给你0.5为Math.random()并不会返回1 。 这篇文
This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers If you want the values 1 or 2 with equal probability, then int temp = (Math.random() <= 0.5) ? 1 : 2; int temp = (Math.random() <= 0.5) ? 1 : 2; is all you need. That gives you a 1 or a 2, each with probability 1/2. int tmp = (int) ( Math.random() * 2 + 1); // will
这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 如果你想以相等的概率得到值1或2,那么int temp = (Math.random() <= 0.5) ? 1 : 2; int temp = (Math.random() <= 0.5) ? 1 : 2; 是你所需要的全部。 这给你一个1或2,每个概率为1/2。 int tmp = (int) ( Math.random() * 2 + 1); // will return either 1 or 2 Math.random()从[0, 1) Math.random()返回数字。 由于(int)总是向
This question already has an answer here: How do I generate random integers within a specific range in Java? 57 answers If you want to generate a number from range [0, 9999], you would use random.nextInt(10000) . Adding leading zeros is just formatting: String id = String.format("%04d", random.nextInt(10000)); A Java int will never have leading 0 (s). You'll need a String for that.
这个问题在这里已经有了答案: 如何在Java中的特定范围内生成随机整数? 57个答案 如果你想从范围[0,9999]中产生一个数字,你可以使用random.nextInt(10000) 。 添加前导零只是格式化: String id = String.format("%04d", random.nextInt(10000)); Java int永远不会有前导0 (s)。 你需要一个String 。 你可以使用String.format(String, Object...)或( PrintStream.printf(String, Object...) ) Random rand = ne
Possible Duplicate: Java: generating random number in a range How do I generate a random value between two numbers. Random.nextInt() gives you between 0 and the passed value. How do I generate a value between minValue and a maxValue Write a method like: public static int getRandom(int from, int to) { if (from < to) return from + new Random().nextInt(Math.abs(to - from));
可能重复: Java:在范围内生成随机数 如何在两个数字之间生成一个随机值。 Random.nextInt()给你介于0和传递的值之间。 如何在minValue和maxValue之间生成一个值 写下如下的方法: public static int getRandom(int from, int to) { if (from < to) return from + new Random().nextInt(Math.abs(to - from)); return from - new Random().nextInt(Math.abs(to - from)); } 这也考虑到了事实,
Possible Duplicate: Java: generating random number in a range I need a little help. What code would I use to create a random number that is 5 digits long and starts with either 1 or 2? in order to use as a company employees ID. Thanks!! Depending on how you approach the problem something like that: public int gen() { Random r = new Random( System.currentTimeMillis() ); return
可能重复: Java:在范围内生成随机数 我需要一点帮助。 我将使用什么代码创建一个长度为5位的随机数,并以1或2开头? 以便用作公司员工ID。 谢谢!! 取决于你如何解决这个问题: public int gen() { Random r = new Random( System.currentTimeMillis() ); return 10000 + r.nextInt(20000); } 或者类似的东西(你可能想要的方法的随机对象的瞬时,但我只是为了简单起见): public int gen() { Ran
Possible Duplicate: Java: generating random number in a range I want to generate random numbers using java.util.Random(arg); The only problem is, the method can only take one argument, so the number is always between 0 and my argument. Is there a way to generate random numbers between (say) 200 and 500? Random rand = new Random(seed); int random_integer = rand.nextInt(upperbound-lowerboun
可能重复: Java:在范围内生成随机数 我想用随机数生成 java.util.Random(arg); 唯一的问题是,该方法只能带一个参数,所以数字总是在0和我的参数之间。 有没有办法在(比方说)200和500之间生成随机数字? Random rand = new Random(seed); int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound; 首先,你必须创建一个Random对象,例如: Random r = new Random(); 然后,如果你想要一个int值,