473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

show Hex Numbers

I want to display hex numbers in a <input type-"text:> how do I do it?
Jan 20 '06 #1
21 16302
News wrote on 20 jan 2006 in comp.lang.javascript:
I want to display hex numbers in a <input type-"text:> how do I do it?


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)
Jan 20 '06 #2
VK

News wrote:
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. 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;
}

Jan 20 '06 #3


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


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 20 '06 #4
VK wrote on 20 jan 2006 in comp.lang.javascript:
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.


No, binary.

Only the displaying of numbers converts to decimal.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #5
VK

Martin Honnen wrote:
var string = n.toString(16).toUpperCase();


That is a great hint!
Thank you.

Jan 20 '06 #6
VK said the following on 1/20/2006 11:43 AM:

<snip>
But internally it treats all numbers in decimal form.


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 20 '06 #7
On Fri, 20 Jan 2006 12:04:55 -0500, Randy Webb
<Hi************@aol.com> wrote:
VK said the following on 1/20/2006 11:43 AM:

<snip>
But internally it treats all numbers in decimal form.


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.

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
Jan 20 '06 #8

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.242...
News wrote on 20 jan 2006 in comp.lang.javascript:
I want to display hex numbers in a <input type-"text:> how do I do it?


You can just key them in from the keyboard.

3f ff a2 .....

Perhaps you better explain what you really want.

I don't want your help thanks
Jan 20 '06 #9

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:43***********************@newsread2.arcor-online.net...


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


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.

--

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


Wow thanks for this, I will use it and also try to learn what is happening
;-)
Jan 20 '06 #10

"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

News wrote:
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. 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;
}


Thanks for this help I have never tried to dynamically show hex numbers
before I have heaps to learn
Jan 20 '06 #11
Paul Cooper wrote on 20 jan 2006 in comp.lang.javascript:
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!


"Central Data Basic" for the Signetics 2650 microprocessor,
rumored to be coded by a William Gates in the early 1980's,
had excellent BCD support.

It safely ran my company's financials.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #12
VK

Randy Webb wrote:
VK said the following on 1/20/2006 11:43 AM:

<snip>
But internally it treats all numbers in decimal form.


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.


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?

Maybe because too many people here have derogatory adjectives to
describe other people?

Or because everyone is eventually getting sick of public beatdown for
each missed fn dot in the post?

Or because some self-appointed experts here cannot go to bed w/o
prooving to a newcomer how stupid and ignorant (s)he is?

Or because some experts got crazy over ECMA and want everyone to use
Netscape 4.x tools for forever and until ECMAScript 4 will come. (It
will never come, at least not withing any acceptable period of time.
You did not get it yet? Or just pretending that you do not understand?)
....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! ;-)

Jan 20 '06 #13
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.
(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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #14
VK wrote:
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?
[...]


If you like it better there, then post there.
PointedEars
Jan 20 '06 #15
VK

Evertjan. wrote:
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>


OK

Jan 20 '06 #16
JRS: In article <bl********************************@4ax.com>, dated
Fri, 20 Jan 2006 17:38:47 remote, seen in news:comp.lang.javascript,
Paul Cooper <pa*********@invalid.bas.ac.uk> posted :

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!


If you have a PC, you can find BCD in the RTC - the bit that keeps the
time when the power is off.

If you look for example in Delphi newsgroups, you can see how, with the
/savior faire/ of the average company-applications programmer, much
trouble is caused by the lack of true decimal arithmetic in languages.

Via sig line 3, LONGCALC will do decimal integer arithmetic up to about
+-10^65520, and Hex to +-16^65520. That's the 16-bit version; the
32-bit EXE, via sig line 2 IIRC, goes to exponent 99999999. Slowly.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jan 20 '06 #17
Paul Cooper wrote:
On Fri, 20 Jan 2006 12:04:55 -0500, Randy Webb
<Hi************@aol.com> wrote:
VK said the following on 1/20/2006 11:43 AM:

<snip>
But internally it treats all numbers in decimal form.

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.

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.


No, it is /not/ supported by any IBM 360-zSeries Fortran compiler.

It is supported by the IBM 360-zSeries compilers for PL/I, COBOL, RPG,
and (mainly in order to deal with existing data files) by an ad-hoc
language extension to C. It /should/ be supported in Ada 95, but I don't
know for sure that it is.

Older computers used other means to achieve decimal:

Excess-3. Like BCD, but 0-9 are encoded as BCD 3-12 (it slightly
simplifies subtraction).

Three-of-five: five bits, three 1, two 0. (Only ten of the 32 possible
five-bit figures are available.)

Biquinary: seven bits. The first two are either 01 or 10; the remaining
five are 10000, 01000, 00100, 00010, or 00001.

Decimal calculations /are/ necessary in banking, etc. However, direct
decimal implementation is not the only way to do decimal calculations.
You can, for example, calculate everything in cents, or mills, using
binary integers. However, this can involve extra multiplications and
divisions by powers of ten to keep things in order. The most common Ada
compiler for Windows uses this method to simulate decimal.

Actually, the i86 architecture supports BCD in a fossilized form dating
back to Intel's original intention of putting the 4004 chip into desk
calculators. The instructions are only helper instructions that need to
be put into subroutines, and I don't know of any compiler that uses them
to implement decimal arithmetic, but they're there.

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
Jan 21 '06 #18
On 2006-01-20, Paul Cooper <pa*********@invalid.bas.ac.uk> wrote:
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!


I believe it's still used in some pocket calculators.

Bye.
Jasen
Jan 21 '06 #19
On 2006-01-21, John W. Kennedy <jw*****@attglobal.net> wrote:
Older computers used other means to achieve decimal:

Excess-3. Like BCD, but 0-9 are encoded as BCD 3-12 (it slightly
simplifies subtraction).
used for serial links mainly IIRC
Three-of-five: five bits, three 1, two 0. (Only ten of the 32 possible
five-bit figures are available.)
ISTR used for bar codes,
Actually, the i86 architecture supports BCD in a fossilized form dating
back to Intel's original intention of putting the 4004 chip into desk
calculators. The instructions are only helper instructions that need to
be put into subroutines, and I don't know of any compiler that uses them
to implement decimal arithmetic, but they're there.


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.
Bye.
Jasen
Jan 21 '06 #20
Jasen Betts wrote:
On 2006-01-21, John W. Kennedy <jw*****@attglobal.net> wrote:
Actually, the i86 architecture supports BCD in a fossilized form dating
back to Intel's original intention of putting the 4004 chip into desk
calculators. The instructions are only helper instructions that need to
be put into subroutines, and I don't know of any compiler that uses them
to implement decimal arithmetic, but they're there.
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.


Decimal arithmetic is part of Ada 95, but GNAT for Windows (the Ada
compiler built on GCC) implements decimal arithmetic as decimal-scaled
binary, rather than use the Intel decimal-help instructions.

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
Jan 21 '06 #21
Martin Honnen wrote:


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

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.


Hey, thanks Martin. I wanted a script to generate hex keys based on a
phrase entered by a user, here's (part of) what I came up with:

<script type="text/javascript">

function toHex(el)
{
var t = el.value;
var r = '';
for (var i=0, len=t.length; i<len; ++i){
r += t.charCodeAt(i).toString(16);
}
document.getElementById('hexVal').innerHTML = r;
}

</script>

<input type="text" onkeyup="toHex(this);">
<div id="hexVal"></div>
I prefer letters in the hex values to be lower case, it makes them stand
out more.
--
Rob
Jan 23 '06 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: koray | last post by:
hi, i need to show large numbers seperated by commas. since i'm using variables from speedscript, i cannot know their values, since the user should enter them. how should i code to show these...
5
by: drew | last post by:
I am reading my textbook and doing the code examples in MS Visual C++ 6.0 for my next class beginning Jan 05. The textbook examples show line numbers. How do I get line numbers to show as I type in...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
5
by: John Smith | last post by:
How are line numbers enabled in the VC++ 6.0 IDE editor?
1
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
2
by: Otto | last post by:
Hi I'm migrating from Access til MySQL. Works fine so far - but one thing is nearly killing me: I got the count of total records in a variabel - (antalRecords) I got the count for the Field Q1...
3
by: WyattM | last post by:
I am using VB.Net 2005 and I have been working on trying to get this project to convert the numbers entered on form1 in TxtBxAmount to words on formcheck in TxtBxAmount3 but I can not figure it out....
19
by: muddasirmunir | last post by:
can any body tell me how to round numbers in in exactly two decimal places i had use a function round(text1.text,2) but whenever there is zere(0) at the end it does not show it for eg for round...
8
by: Abedin | last post by:
I have 9 digits in form of "111020402". Then I have another 9 digits in form of "111020403". I have 100,000 records of these two 9-digit numbers. I want to filter this as follows: District:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.