使用Groovy脚本生成随机16位十六进制数

使用Groovy脚本生成随机16位十六进制数,groovy,hex,soapui,Groovy,Hex,Soapui,我正在尝试生成16位随机十六进制数 import org.apache.commons.lang.RandomStringUtils; def randomhex = RandomStringUtils.randomNumeric(16); log.info randomhex def result = Integer.toHexString(randomhex); log.info result 应为:结果应为随机16位十六进制数。 e、 g:328A6D01F9FF12E0 实际值: gr

我正在尝试生成16位随机十六进制数

import org.apache.commons.lang.RandomStringUtils;
def randomhex = RandomStringUtils.randomNumeric(16);
log.info randomhex
def result = Integer.toHexString(randomhex);
log.info result
应为:结果应为随机16位十六进制数。 e、 g:328A6D01F9FF12E0

实际值
groovy.lang.MissingMethodException:没有方法签名:静态java.lang.Integer.toHexString()适用于参数类型:(java.lang.String)值:[3912632387180714]可能的解决方案:toHexString(int)、toString()、toString()、toString()、toString(int)、toString(int)、toString(int、int)第行错误:存储16位十六进制数需要9位

64位,该十六进制数大于整数支持。可以改为使用Long(Java 8中添加了
toUnsignedString
方法):

另一种可能的方法是生成从0到16的16个随机整数,并将结果连接到一个字符串中

def r = new Random()
def result = (0..<16).collect { r.nextInt(16) }
                     .collect { Integer.toString(it, 16).toUpperCase() }
                     .join()

我想错误信息很清楚吧?ToHexString接受一个int,但您正在传递一个字符串。转换为整数后。导入org.apache.commons.lang.RandomStringUtils;def randomhex=RandomStringUtils.randomNumeric(16.toInteger();log.info randomhex def result=Integer.tohextString(randomhex);log.info结果。它抛出错误-'java.lang.NumberFormatException:对于输入字符串:“0192171878046802”错误位于第2行
def r = new Random()
def result = (0..<16).collect { r.nextInt(16) }
                     .collect { Integer.toString(it, 16).toUpperCase() }
                     .join()
def result = UUID.randomUUID()
                 .toString()
                 .split('-')[-1..-2]
                 .join()
                 .toUpperCase()