所以我有一个这样的字符串:
char numbers[] // string of numbers seperated by , and at the end ]
unsigned long long arr[length] // get the correct length
for(int i = 0; i < length; i++){
arr[i]=strtoull(numbers,???,10)// pass correct arguments
}
这是一个例子,数组中可以有更多的数字,但它肯定会以]结尾。
所以现在我需要将它转换为无符号长long数组。 我知道我可以使用strtoull(),但它需要3个参数,我不知道如何使用第二个参数。另外我想知道如何让我的阵列具有合适的长度。我希望我的代码看起来像这样,但不是伪代码,而是C:
var node= document.getElementById("elementId");
var canvas = document.createElement("canvas");
canvas.height = node.offsetHeight;
canvas.width = node.offsetWidth;
var name = "test.png"
rasterizeHTML.drawHTML(node.outerHTML, canvas)
.then(function (renderResult) {
if (navigator.msSaveBlob) {
window.navigator.msSaveBlob(canvas.msToBlob(), name);
} else {
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = canvas.toDataURL();
a.download = name;
a.click();
document.body.removeChild(a);
}
});
C可以这样做吗?
答案 0 :(得分:2)
strtoull
的第二个参数是指向char *
的指针,它将接收指向字符串参数中数字后面的第一个字符的指针。第三个参数是用于转换的基础。 Base 0允许0x
前缀指定十六进制转换,0
前缀指定八进制,就像C整数文字一样。
您可以这样解析您的行:
extern char numbers[]; // string of numbers separated by , and at the end ]
unsigned long long arr[length] // get the correct length
char *p = numbers;
int i;
for (i = 0; i < length; i++) {
char *endp;
if (*p == ']') {
/* end of the list */
break;
}
errno = 0; // clear errno
arr[i] = strtoull(p, &endp, 10);
if (endp == p) {
/* number cannot be converted.
return value was zero
you might want to report this error
*/
break;
}
if (errno != 0) {
/* overflow detected during conversion.
value was limited to ULLONG_MAX.
you could report this as well.
*/
break;
}
if (*p == ',') {
/* skip the delimiter */
p++;
}
}
// i is the count of numbers that were successfully parsed,
// which can be less than len