2017-12-06 3 views
0

16 진수 16 진수 16 진수를 생성하려고합니다.Groovy 스크립트를 사용하여 임의의 16 자리 16 진수 생성

import org.apache.commons.lang.RandomStringUtils; 
def randomhex = RandomStringUtils.randomNumeric(16); 
log.info randomhex 
def result = Integer.toHexString(randomhex); 
log.info result 

는 기대 값 : 결과는 임의의 16 자리 16 진수이어야한다. 예 : 328A6D01F9FF12E0

실제 : groovy.lang.MissingMethodException : 아니오 방법 서명 (java.lang.String의) 값 : : 정적 java.lang.Integer.toHexString()는 인수의 형태에 적용 3912632387180714] 가능한 해결책 : toHexString (int), toString(), toString(), toString(), toString (int), toString (int, int) 행의 오류 : 9

+0

오류 메시지가 분명할까요? ToHexString은 int를 취하지 만 문자열을 전달합니다. –

+0

정수로 변환 한 후. import org.apache.commons.lang.RandomStringUtils; def randomhex = RandomStringUtils.randomNumeric (16) .toInteger(); log.info randomhex def result = Integer.toHexString (randomhex); log.info 결과. 오류가 발생합니다 - 'java.lang.NumberFormatException : 입력 문자열의 경우 : "0192171878046802"라인 오류 : 2' – rAJ

답변

1

16 비트를 저장하려면 64 비트가 필요합니다. 정수보다 큰 16 진수 숫자입니다. 긴합니다 (toUnsignedString 방법은 자바 8에서 추가되었다) 대신에 사용될 수있다 :

def result = Long.toUnsignedString(new Random().nextLong(), 16).toUpperCase() 

또 다른 잠재적 인 접근은 0 16 16 개 임의의 정수를 생성하고, 문자열로 결과를 결합하는 것입니다.

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

또 다른 접근법은 임의의 UUID를 활용하여 그 중에서 마지막 16 자리를 잡는 것입니다.

def result = UUID.randomUUID() 
       .toString() 
       .split('-')[-1..-2] 
       .join() 
       .toUpperCase()