在 JavaScript 中将 int64 值转换为 Number 对象

发布于 2024-08-31 14:18:52 字数 877 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪推晚风 2024-09-07 14:18:52

alert(Number(String(foo)) * 2);

如果您的 COM 对象正确实现了 toString (或带有“string”提示的 valueOf),则应该这样做(但是见下文) 显然,如果您的 innerHTML 技巧有效的话,它确实如此 - 因为当您将 foo 分配给 innerHTML 时,将 COM 对象转换为相同的过程字符串的出现与 String(foo) 相同)。

来自第五版 ECMAScript 规范的第 15.5.1 节:

当 String 作为函数而不是构造函数调用时,它会执行类型转换。

和第 15.7.1 节

当 Number 作为函数而不是构造函数调用时,它会执行类型转换

可能值得尝试 Number(foo) * 2 来确定,但我认为不会工作(看起来你的COM对象只处理到字符串的转换,而不是数字,这并不奇怪或不合理)。


编辑如果String(foo)失败,请尝试:

alert(Number("" + foo) * 2);

非常惊讶于你的innerHTML技巧有效但 String(foo) 抛出错误。希望 "" + foo 能够触发与您的 innerHTML 技巧相同的隐式转换。


编辑 好吧,这个 COM 对象确实很奇怪。我接下来的两次攻击:

alert(("" + foo) * 2);

使用所有隐式转换(将对象添加到字符串中会将对象转换为字符串;将 * 运算符应用于字符串会将其转换为数字)。

或者,我们可以显式但间接地进行字符串->数字转换:

alert(parseInt("" + foo) * 2);

This:

alert(Number(String(foo)) * 2);

should do it (but see below), if your COM object implements toString (or valueOf with the "string" hint) correctly (and apparently it does, if your innerHTML trick works -- because when you assign foo to innerHTML, the same process of converting the COM object to a string occurs as with String(foo)).

From Section 15.5.1 of the 5th Edition ECMAScript spec:

When String is called as a function rather than as a constructor, it performs a type conversion.

And Section 15.7.1

When Number is called as a function rather than as a constructor, it performs a type conversion

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:

alert(Number("" + foo) * 2);

I'm very surprised that your innerHTML trick is working but String(foo) is throwing an error. Hopefully "" + foo will trigger the same implicit conversion as your innerHTML trick.


Edit Okay, this COM object is being very strange indeed. My next two salvos:

alert(("" + foo) * 2);

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:

alert(parseInt("" + foo) * 2);
写给空气的情书 2024-09-07 14:18:52

哎呀。好吧,如果由于主机对象的奇怪行为而没有显式转换起作用,那么让我们尝试隐式转换:

var n= +(''+foo);

我假设您不介意目标类型 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:

var n= +(''+foo);

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 a double, so you only get 52 bits of mantissa).

樱&纷飞 2024-09-07 14:18:52

马特,从评论到其他答案,我怀疑您正在某种循环中运行此代码。如果是这样,请确保在尝试转换之前检查返回值是否为 null。

var foo = MyCOMObject.GetInt64Value(); 
if (foo == null) {
  foo = 0; // Or something else
}

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.

var foo = MyCOMObject.GetInt64Value(); 
if (foo == null) {
  foo = 0; // Or something else
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文