go - Converting a C++ snippet to Golang -


i'm trying covert following c++ snippet golang, i'm not having luck getting syntax correct. here's c++ snippet looks like:

v8::string::utf8value reqstringobj(args[1]); const char *reqstring = *reqstringobj;  char hex[3] = {reqstring[strlen(reqstring) - 2], reqstring[strlen(reqstring) - 1], '\0'}; unsigned char requestid = (unsigned char)strtoul (hex, 0, 16); printf("requestid is: %d\n", requestid); 

here's have far via go version:

reqstr := "somerandomstringthatihave" hex := []uint8{reqstr[len(reqstr)-2], reqstr[len(reqstr)-1], '\u0027'}  requestid := ????? 

i'm not sure how convert casted strtoul function noted in c++ function make work in same way via go version. ideas?

you can accomplish rather using strconv.parseuint:

reqstr := "jfd0jifdgfa" if len(reqstr) <= 1 {   panic("reqstr not long enough") } requestid, err := strconv.parseuint(reqstr[len(reqstr) - 2:], 16, 64) if err != nil {   panic(err) } 

keep in mind final 2 characters of reqstr must valid hex characters, otherwise above code panic.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -