N
News
I want to display hex numbers in a <input type-"text:> how do I do it?
I want to display hex numbers in a <input type-"text:> how do I do it?
News said:I want to display hex numbers in a <input type-"text:> how do I do it?
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.
But internally it treats all numbers in decimal form.
VK said the following on 1/20/2006 11:43 AM:
Do you ever stop and wonder why most of the people here have derogatory
adjectives to describe you?
Computers have no sense of "decimal form" internally, it only knows of
binary and any other base is computed when needed.
I don't want your help thanksEvertjan. said:News wrote on 20 jan 2006 in comp.lang.javascript:
You can just key them in from the keyboard.
3f ff a2 .....
Perhaps you better explain what you really want.
Martin Honnen said:Such an input displays strings, to set the input use
inputElement.value = string
If you have a number e.g.
var n = 255;
and want to get the hexadecimal representation then you can use
var string = n.toString(16);
or
var string = n.toString(16).toUpperCase();
so perhaps
inputElement.value = n.toString(16).toUpperCase();
is what you want.
VK said: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;
}
Well, Binary Coded Decimal was used once upon a time to encode decimal
numbers (use 4 bits per decimal digit, but only use the numeric values
0-9). A bit of a kludge, and still using binary coding for the digits,
but the arithmetic etc was all done in base 10. I gather it was
regarded as essential in the banking industry at one time - it avoided
rounding errors implicit in binary computation. Most of the old IBM
Fortran IV (G, H and H(ext)) and 66 compilers supported it.
I haven't seen it for a very long time, but it certainly existed!
Randy said:VK said the following on 1/20/2006 11:43 AM:
Do you ever stop and wonder why most of the people here have derogatory
adjectives to describe you?
Computers have no sense of "decimal form" internally, it only knows of
binary and any other base is computed when needed.
...computers are using binary format for internal data. Great thank to
you for putting light on it for me. But I'm still not so stupid as it
may seem: I know
(and I got it completely by myself!) that computers
are running on the electicity! ;-)
VK said:Do you ever stop and wonder why <alt.comp.lang.javascript> appeared and
why so many people are posting there? And a lot of them first seen at
comp.lang.javascript but never come back?
[...]
Evertjan. said:VK wrote on 20 jan 2006 in comp.lang.javascript:
...computers are using binary format for internal data. Great thank to
you for putting light on it for me. But I'm still not so stupid as it
may seem: I know
Here you are wrong, VK, [and I am not talking about any stupidness],
"internal data" is not the same as "internal number format".
As shown elsewhere in this thread, BCD is an internal decimal
representation and storage of numbers, that is quite capable of having
internal Math functions.
As long as we are talking integers, the difference is minimal,
but with fraction or floating point math, it is quite important.
OK
(and I got it completely by myself!) that computers
are running on the electicity! ;-)
Some computers are running on light beams and I have even seen a working
model running on compressed air having real flip-flop-gates.
Mechanical computers (Babbage!) where quitwe sophisticated.
<http://www.memagazine.org/backissues/sept03/features/adding/50.jpg>
Paul Cooper said:Well, Binary Coded Decimal was used once upon a time to encode decimal
numbers (use 4 bits per decimal digit, but only use the numeric values
0-9). A bit of a kludge, and still using binary coding for the digits,
but the arithmetic etc was all done in base 10. I gather it was
regarded as essential in the banking industry at one time - it avoided
rounding errors implicit in binary computation. Most of the old IBM
Fortran IV (G, H and H(ext)) and 66 compilers supported it.
I haven't seen it for a very long time, but it certainly existed!
Paul said:Well, Binary Coded Decimal was used once upon a time to encode decimal
numbers (use 4 bits per decimal digit, but only use the numeric values
0-9). A bit of a kludge, and still using binary coding for the digits,
but the arithmetic etc was all done in base 10. I gather it was
regarded as essential in the banking industry at one time - it avoided
rounding errors implicit in binary computation. Most of the old IBM
Fortran IV (G, H and H(ext)) and 66 compilers supported it.
Most processors I've encountered have something like that, if BCD is part of
Ada I'd be suprised if an Ada compiler didn't emit those instructions.
Martin said:Such an input displays strings, to set the input use
inputElement.value = string
If you have a number e.g.
var n = 255;
and want to get the hexadecimal representation then you can use
var string = n.toString(16);
or
var string = n.toString(16).toUpperCase();
so perhaps
inputElement.value = n.toString(16).toUpperCase();
is what you want.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.