This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Is there a method that returns a hexidecimal representation of an integer that isn't a string? If I said: int i = 55; someMethod(i); and I would get back 37? (Integer.toHexString won't work since that returns a String, not a number)
What exactly were you after? How would you incorporate hex digits a - f if you returned (say) an int? The way a number is stored in memory and the way it's represented are very distinct - toString returns base 10, toHexString returns base 16 etc, but internally ints are stored in base 2. When you get an int return, the implication is that it's the value you're concerned about, not the representation. Cheers, --Tim
Joined: Mar 17, 2004
I'm reading a file...if the input is a 55, I want to write a 37 to a second file. ....but the way you explain it, that's not doable since what I'm actually writing will be "0000 0011 0111" anyway....
Joined: Mar 15, 2004
Oops - I was unclear. You can read and write however you want - base 10, base 16, base 2, provided you know how to convert data appropriately (which was your original question). As you know, in memory all data is stored in binary. The point of this is that any one representation (eg, base 10, that returned by Integer.toString()) is just one view of the internal representation, according to some rules. What we need to do for your problem is to determine what the rules are in your case. I think I read your question wrong originally. One point I tried to make, which is still obliquely relevant, is this: When you get an return value of type int, you are getting a number, but you don't get a representation. When you get a String, you _are_ getting a representation, but this can mean a loss of the original meaning of the number. That would make more sense with some examples...if it's unclear I'll happily write them. Now, back to your original question... To do the conversion from one file to the other, is there a problem with toHexString()? As you say, it returns a String - it should be fine to write a String to a file with a PrintWriter. If it's not OK, and you really want a function that returns an int, what should the return value be for 10 (which has hex value 'a'?) Hope all this helps...or at least raises more questions :-)
--Tim [ April 14, 2004: Message edited by: Tim West ] [ April 14, 2004: Message edited by: Tim West ]
Originally posted by Will Carpenter: Is there a method that returns a hexidecimal representation of an integer that isn't a string? If I said: int i = 55; someMethod(i); and I would get back 37? (Integer.toHexString won't work since that returns a String, not a number)
Just take the returned String from toHexString and turn it into an int.
The last code could be changed to and it will work. This initializes a 32 bit word of memory to a 32 bit signed binary number: 00000000 00000000 00000000 00001010 At this point, the number exists only as electronic charges inside your computer. You can't see it on your screen or printer. Integer.toHexString() then converts that word to four 16 bit unsigned binary numbers (characters):
It's still electronic charges in memory, but could easily be sent to your display using System.out.println(). On the way there, these numbers will be changed into the human-readable graphics 000a. Integer.parseInt(,16) takes the four character string, interprets it as a 4 digit hexadecimal number, and returns a 32 bit signed binary number: 00000000 00000000 00000000 00001010 Remember that the java methods that put text information where we can see it require a String. Primitive numeric types are always converted to String objects before being sent to the display or printer. You can control whether a String is the decimal, binary, or hexadecimal representation of the binary number, because those are some external (human-friendly) ways of representing a number. The internal forms, like int, are always base 2 and cannot be viewed without first being converted into a String and then being sent to a display or printer. Of course, you started out with the human readable 10, but the javac compiler changed that into a binary number when it compiled your program into bytecode. So "int i = 10" actually moves a 32 bit binary number into the 32 bit memory word reserved for variable "i". I'm not sure if I answered the question you had in mind. If not, please rephrase it. [ April 14, 2004: Message edited by: Mike Gershman ]
SCJP 1.4, SCWCD in process
Joined: Mar 15, 2004
Mike, I considered including an example of parseInt(int, int) ... but decided against it to save myself typing ... good explanation though. I'm still not sure whether we've been much help to the original poster, but I think we need a clearer spec of his intentions before we could comment meaningfully further - it's pretty clear that 'return int' and 'return a human-readable hex representation' are mutually exclusive. --Tim