We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Int to hex (Read 671 times)
Int to hex
Feb 23rd, 2009, 9:57pm
 
I'm having a lot of trouble developing a code that turns an integer input n into its hexadecimal counterpart as a string.  So I need to develop a function String int2hex(int n).  This is a very small part of a much larger project, and I don't know why this is giving me soo much trouble.  If anyone has some simple code they could show me, and or give me some rather specific instructions, it would be greatly appreciated.
Re: Int to hex
Reply #1 - Feb 23rd, 2009, 11:04pm
 
> So I need to develop a function String int2hex(int n).

no, you don't

http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html

static String toHexString(int i)
Creates a string representation of the integer argument as an unsigned integer in base 16.
Re: Int to hex
Reply #2 - Feb 23rd, 2009, 11:22pm
 
Do you know where I can find the more complex code behind the built in function?  I need to make it from scratch, or if you know how to quickly do so...


Thanks for the quick reply
Re: Int to hex
Reply #3 - Feb 24th, 2009, 7:43am
 
Homework Otherwise, just use hex().

If you really need to do it manually, use % 16 to get each digit (then / 16 to drop it -- can do both with bit arithmetic too) and if it is above 9, get the letter from an array (some people just make an array of 16 characters). Concatenate all the digits in the right order.
Re: Int to hex
Reply #4 - Feb 24th, 2009, 4:54pm
 
Thank you Philo, your always very helpful.  Could you explain the 16 characters in an array aspect of the problem?  Still pretty new to Processing, and having a little trouble with some of the basics.

Thanks again
Re: Int to hex
Reply #5 - Feb 25th, 2009, 12:05am
 
String[] hexDigits = { "0", "1", ... "9", "A", ... "F" };
Page Index Toggle Pages: 1