在 JavaScript 中将 int64 值转换为 Number 对象
我有一个 COM 对象,它有一个返回 unsigned int64 (VT_UI8) 值的方法。我们有一个 HTML 页面,其中包含一些 JavaScript,可以加载 COM 对象并调用该方法,以检索值,如下所示:
var foo = MyCOMObject.GetInt64Value();
该值可以轻松地在消息对话框中显示给用户,使用:
alert(foo);
或显示在页面上by:
document.getElementById('displayToUser').innerHTML = foo;
但是,我们不能将此值用作数字(例如,如果我们尝试将其乘以 2),页面不会抛出“Number Expected”错误。如果我们检查“typeof(foo)”,它会返回“unknown”。
我通过执行以下操作找到了解决方法:
document.getElementById('displayToUser').innerHTML = foo;
var bar = parseInt(document.getElementById('displayToUser').innerHTML);
alert(bar*2);
我需要知道的是如何使该过程更加高效。具体来说,有没有一种方法可以将 foo 显式转换为字符串,而不是必须将某些文档元素的innerHTML 设置为 foo 然后从中检索它。我不介意这样称呼:
alert(parseInt((string)foo) * 2);
如果有一种方法可以直接将 int64 转换为数字,而不需要经过字符串转换,那就更好了,但我对此不抱太大希望。
I have a COM object which has a method that returns an unsigned int64 (VT_UI8) value. We have an HTML page which contains some JavaScript which can load the COM object and make the call to that method, to retrieve the value as such:
var foo = MyCOMObject.GetInt64Value();
This value can easily be displayed to the user in a message dialog using:
alert(foo);
or displayed on the page by:
document.getElementById('displayToUser').innerHTML = foo;
However, we cannot use this value as a Number (e.g. if we try to multiply it by 2) without the page throwing "Number expected" errors. If we check "typeof(foo)" it returns "unknown".
I've found a workaround for this by doing the following:
document.getElementById('displayToUser').innerHTML = foo;
var bar = parseInt(document.getElementById('displayToUser').innerHTML);
alert(bar*2);
What I need to know is how to make that process more efficient. Specifically, is there a way to cast foo to a String explicitly, rather than having to set some document element's innerHTML to foo and then retrieve it from that. I wouldn't mind calling something like:
alert(parseInt((string)foo) * 2);
Even better would be if there is a way to directly convert the int64 to a Number, without going through the String conversion, but I hold out less hope for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(
如果您的 COM 对象正确实现了
toString
(或带有“string”提示的valueOf
),则应该这样做(但是见下文) 显然,如果您的innerHTML
技巧有效的话,它确实如此 - 因为当您将foo
分配给innerHTML
时,将 COM 对象转换为相同的过程字符串的出现与String(foo)
相同)。来自第五版 ECMAScript 规范的第 15.5.1 节:
和第 15.7.1 节
可能值得尝试
Number(foo) * 2
来确定,但我认为不会工作(看起来你的COM对象只处理到字符串的转换,而不是数字,这并不奇怪或不合理)。编辑如果
String(foo)
失败,请尝试:我非常惊讶于你的
innerHTML
技巧有效但String(foo)
抛出错误。希望"" + foo
能够触发与您的innerHTML
技巧相同的隐式转换。编辑 好吧,这个 COM 对象确实很奇怪。我接下来的两次攻击:
使用所有隐式转换(将对象添加到字符串中会将对象转换为字符串;将
*
运算符应用于字符串会将其转换为数字)。或者,我们可以显式但间接地进行字符串->数字转换:
This:
should do it (but see below), if your COM object implements
toString
(orvalueOf
with the "string" hint) correctly (and apparently it does, if yourinnerHTML
trick works -- because when you assignfoo
toinnerHTML
, the same process of converting the COM object to a string occurs as withString(foo)
).From Section 15.5.1 of the 5th Edition ECMAScript spec:
And Section 15.7.1
It may be worth trying just
Number(foo) * 2
to make sure, but I don't think it'll work (it seems like your COM object only handles conversion to String, not Number, which isn't surprising or unreasonable).Edit If
String(foo)
is failing, try:I'm very surprised that your
innerHTML
trick is working butString(foo)
is throwing an error. Hopefully"" + foo
will trigger the same implicit conversion as yourinnerHTML
trick.Edit Okay, this COM object is being very strange indeed. My next two salvos:
That uses all implicit conversions (adding an object to a string converts the object to a string; applying the
*
operator to a string converts it to a number).Alternately, we can make the string->number conversion explicit but indirect:
哎呀。好吧,如果由于主机对象的奇怪行为而没有显式转换起作用,那么让我们尝试隐式转换:
我假设您不介意目标类型
Number
不涵盖int64 的完整值范围(它是一个 double,因此您只能得到 52 位尾数)。Eek. Well if none of the explicit conversions are working because of the strange behaviour of the host object, let's try the implicit ones:
I'm assuming you don't mind that the target type
Number
doesn't cover the full range of values of an int64 (it's adouble
, so you only get 52 bits of mantissa).马特,从评论到其他答案,我怀疑您正在某种循环中运行此代码。如果是这样,请确保在尝试转换之前检查返回值是否为 null。
Matt, from the comments to other answers, I suspect you're running this code in some sort of loop. If so, make sure that you check the returned value for null before trying your conversions.