显示十六进制数字 [英] show Hex Numbers

查看:92
本文介绍了显示十六进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在< input type-" text中显示十六进制数字:>我怎么做?

I want to display hex numbers in a <input type-"text:> how do I do it?

推荐答案

新闻写于2006年1月20日comp.lang.javascript
News wrote on 20 jan 2006 in comp.lang.javascript:
我想在< input type-" text中显示十六进制数字:>我该怎么做?
I want to display hex numbers in a <input type-"text:> how do I do it?




你可以从键盘上输入它们。


3f ff a2 ... ..


也许你最好解释一下你真正想要的东西。


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)



You can just key them in from the keyboard.

3f ff a2 .....

Perhaps you better explain what you really want.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)




新闻写道:

News wrote:
我想在< input type-" text中显示十六进制数字:>我该怎么做?
I want to display hex numbers in a <input type-"text:> how do I do it?




JavaScript支持0xFFFF格式的十六进制数字文字。

var foo = 0xFFFF; // foo现在是65535十进制


但在内部它会以十进制形式处理所有数字。因此,您可以轻松地将十六进制字符串转换为十进制值:

< input type =" text"名称= QUOT; V1" value =" FFFF"

onblur =" alert(parseInt(this.value,16))">


- 但你不要'没有原生的JavaScript函数用于反向

操作(将十进制整数转换为十六进制字符串)。


有一个自定义函数的海洋为后者。这是

一:


var hD =''0123456789ABCDEF'';

函数dec2hex(d){

var h = hD.substr(d& 15,1);

while(d> 15){

d>> = 4;

h = hD.substr(d& 15,1)+ h;

}

返回h;

}



JavaScript supports hexadecimal numeric literals in the form 0xFFFF.
var foo = 0xFFFF; // foo is now 65535 decimal

But internally it treats all numbers in decimal form. So you can easily
convert a hex string into decimal value:
<input type="text" name="v1" value="FFFF"
onblur="alert(parseInt(this.value,16))">

- but you don''t have a native JavaScript function for the reverse
action (convert decimal integer into hex string).

There is an ocean of custom functions written for the latter. Here is
one:

var hD=''0123456789ABCDEF'';
function dec2hex(d) {
var h = hD.substr(d&15,1);
while (d>15) {
d>>=4;
h=hD.substr(d&15,1)+h;
}
return h;
}





新闻写道:


News wrote:
我要显示< input type-" text中的十六进制数字:>我该怎么做?
I want to display hex numbers in a <input type-"text:> how do I do it?




这样的输入显示字符串,设置输入用途

inputElement.value = string

如果您有一个数字,例如

var n = 255;

并想获得十六进制表示,那么您可以使用

var string = n.toString(16);



var string = n.toString(16).toUpperCase();

so也许

inputElement.value = n.toString(16).toUpperCase();

就是你想要的。


- -


Martin Honnen
http:// JavaScript.FAQTs.com/


这篇关于显示十六进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆