Erki M.
Erki M.

Reputation: 5072

groovy, string refuses to be a bigInt, is odd instead

I am losing my hair with Groovy lang, it has ruined 4 days for me in a row and it is counting.

What i need to do is to execete an external process, read stdout, get that value and convert it to big int. What i have

def str ='openssl x509 -in "'+ file + '" -serial -noout'    
def command = "cmd /c " + str;
def proc = command.execute();
proc.waitFor();
def response =  "${proc.in.text}"
def result = response.split('=')[1]
log.info result
// outputs: 7434F30AEE5F2001530C7F0C4844E9EE
log.info result.class.name
// outputs: java.lang.String

Ok, very cool, lets go on:

def big = new BigInteger(result.decodeHex())

No-no, cant be done:

java.lang.NumberFormatException: odd number of characters in hex string

Meanwhile in another script:

def numb = '7434F30AEE5F2001530C7F0C4844E9EE'
log.info numb.class.name
// outputs : java.lang.String
b = new BigInteger(numb.decodeHex())
log.info b
// outputs : 154465376439281796222583609645190146542

Upvotes: 1

Views: 381

Answers (1)

Smutje
Smutje

Reputation: 18153

You should trim your result from whitespaces, newlines and stuff like that - and after that, print out the number of characters in this string if they are even or still odd.

Upvotes: 4

Related Questions