Originally posted by Ken Blair:
Why reinvent the wheel? The wrapper classes such as java.lang.Long provide a byteValue() method to get it as a byte and the java.lang.Byte wrapper provides longValue(), intValue(), etc. methods to convert back. I've never had reason to use these so I apologize if it's a stupid suggestion.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
Use the java.nio buffers for this kind of data conversion.
"I'm not back." - Bill Harding, Twister
Adam Zehavi wrote:...so I've made a generic method to perform the task:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Adam Zehavi wrote:I took a long look at many questions like this, and found this post... I didn't like the fact that the conversion code is duplicated for each type, so I've made a generic method to perform the task:
See full post
That method almost certainly contains a serious error in the bit about n-1*8, I am afraid.
Adam Zehavi wrote:I take performance very seriously...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Adam Zehavi wrote:I take performance very seriously...
Then you might be interested to know that
Class<? extends Number> dataType = data.getClass();
if (dataType == Byte.class) {...
takes about 8 times as long as
if (dataType instanceof Byte) {...
(at least on my computer)...and the latter handles nulls.
Campbell Ritchie wrote:
But don’t the Wrapper classes all have a static SIZE field which records the number of bits used? You can use that with << 3 to convert the bits to bytes.
That may hit problems with BigDecimal and BigInteger which extend Number but don’t have the SIZE field.
Yes, I missed that. SorryMike Simmons wrote:I'm confused too. Looks like Adam is already using the SIZE . . .
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|