Google Answers Logo
View Question
 
Q: Convert LARGE decimal numbers to Hex ( No Answer,   5 Comments )
Question  
Subject: Convert LARGE decimal numbers to Hex
Category: Computers > Algorithms
Asked by: ricjohnsoniii-ga
List Price: $55.00
Posted: 09 May 2005 20:34 PDT
Expires: 08 Jun 2005 20:34 PDT
Question ID: 519786
I would like a working implmentation of JavaScript code to convert
arbitarily large decimal integers to hex and to convert the hex back
to decimal.
For example: a HTML form with an input field, a user could type in a
large number: "1234567890123456789012345678901234567890" and press a
button and another field will display the Hexidecimal equivalnt and
another field will use that hex to convert back to decimal in another
field

Clarification of Question by ricjohnsoniii-ga on 10 May 2005 17:23 PDT
Note:  Simply re-creating long division by hand in NOT acceptable.  I
am looking for the MAGIC number (sort of like how square roots are
implmented in binary with the magic number of 20).
I have found a working code up to 10 digits with the magic number of
5, but when I multiply 5 by the remainder, I get an overflow, so I
need a way to re-implment the magic number on Large numbers again.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Convert LARGE decimal numbers to Hex
From: quadraticresidue-ga on 11 May 2005 13:33 PDT
 
I am not sure what you mean by "magic number".  It looks like you
might be referring to the following algorithm for square root:
http://www.dattalo.com/technical/theory/sqrt.html
As the author notes, the algorithm is similar to long division, so I
can't see why you object to using long division.
Subject: Re: Convert LARGE decimal numbers to Hex
From: idoivri-ga on 16 May 2005 00:50 PDT
 
might this help you?

http://www.permadi.com/tutorial/numConvJs/

It's a nice number converter from / to - Dec/Bin/Hex that converts
your number as you type it. I tried it with some numbers I have and it
worked fine.

I.
Subject: Re: Convert LARGE decimal numbers to Hex
From: ricjohnsoniii-ga on 16 May 2005 13:30 PDT
 
Regarding http://www.permadi.com/tutorial/numConvJs/

This number converted is limited to 15 digits.  It uses the internal
MATH methods and native data types to convert to HEX.  I am looking
for something that can convert a LARGE number - assume at least 30
digits.
Subject: Re: Convert LARGE decimal numbers to Hex
From: thefistofthenorthsta-ga on 31 May 2005 18:50 PDT
 
Here's some code I just cooked up.  Should do the trick.  No magic numbers though.


/************************************************
 * This function decodes a decimal string and
 * returns a hexadecimal string.  There is no
 * size limit on the decimal input.
 ************************************************/
function dec2hex(dec)
{
	//Create a buffer for the hexadecimal
	var hex = new Array(
	Math.ceil(
		dec.length * 0.83048202372184058696757985737235
	));
	
	//Initialize as 0
	for(var i=0; i<hex.length; i++)
		hex[i] = 0;
	
	//Create raw binary number in hex
	for(var i=0; i<dec.length; i++)
	{
		//Multiply by 10
		for(var j=0; j<hex.length; j++)
			hex[j] *= 10;
		
		//Add character
		hex[0] += parseInt(dec.charAt(i));
		
		//Carry
		for(var j=0; j < hex.length; j++)
			if(hex[j] > 16)
			{
				hex[j + 1] += Math.floor(hex[j] / 16);
				hex[j] = hex[j] % 16;
			}
	}
	
	//Convert raw binary to hex
	var hex_chars = "0123456789abcdef";
	
	//Strip leading zeros
	var index = hex.length - 1;
	while(hex[index] == 0 && index > 0)
		--index;
	
	//Append the hexadecimal digits
	var result = hex_chars.charAt(hex[0]);
	for(var i=1; i <= index; i++)
		result = hex_chars.charAt(hex[i]) + result;
		
	//Done
	return result;
}
Subject: Re: Convert LARGE decimal numbers to Hex
From: ricjohnsoniii-ga on 03 Jun 2005 11:09 PDT
 
I am sorry, I tried to code this, but it did not work for LARGE
numbers (50+ digits). Can you clarify the code with comments for each
step (I may have coded it wrong).
1) Does the step "//Multiply by 10" encounter an overflow?
2) Why do you work from the lwft (signigicant digits)?
3) This seems the same as long division - can you explain?

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy