如何在 V8 中将整数转换为字符串? [英] How to convert an Integer to a String in V8?
问题描述
我有以下 V8 代码:
I have the following V8 code:
Local<String> getSumString(int32_t a, int32_t b){
int32_t sum = a + b;
return String::Concat(String::New("The sum is: ") , String::New(sum));
}
在上面的函数中我想添加a
和b
,然后想返回一个字符串总和是:CALCULATED_SUM".
In the above function I want to add a
and b
, then want to return a string "The sum is: CALCULATED_SUM " .
我在将计算出的总和转换为字符串以将其与其他字符串连接时遇到问题.
I'm having problems in converting the calculated sum to a String so that it can be concatenated with other String.
推荐答案
你没有说错误的本质,但我猜你的输出字符串的末尾被破坏了,因为你正在实例化一个字符串从一个整数.V8 将该数据解释为 const char * 数据.你应该从你的 int 实例化一个整数.您的最后一行将如下所示:
You don't say the nature of the error, but I'm guessing that the end of your output string is mangled, because you're instantiating a String from an int. V8 is interpreting that data as const char * data. You should instantiate an Integer from your int. Your last line will look like this instead:
return String::Concat(String::New("The sum is: ") , Integer::New(sum));
这篇关于如何在 V8 中将整数转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!