You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Yurelle Gamier (JIRA)" <ji...@apache.org> on 2016/11/24 18:25:58 UTC

[jira] [Commented] (LANG-341) Add number to byte array methods

    [ https://issues.apache.org/jira/browse/LANG-341?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15693971#comment-15693971 ] 

Yurelle Gamier commented on LANG-341:
-------------------------------------

I would like to request that this be given another look, and seriously considered for high priority completion. I believe there is a very large market for this implementation. Adding convenience methods to lang3.Conversion would satisfy a large need in the misc utils space.

Ex:
{code:java}
int i = Conversion.toInt(myByteArray);
byte[] fpData = Conversion.toByteArray(myFloat);
String fpHex = Conversion.toHexString(myDouble);
long l = Conversion.toLong(myHexString);
{code}
etc.

Trying to use the existing methods for individual conversions, results in not-so-pleasant code, due to superfluous parameters and the necessity of creating throw away arrays, rather than just accepting a return. I have repeadedly needed such a solution on several projects (different companies) I have worked on. Each time, trudging through the tedious boilerplate of having to write it from scratch again. For companies which are sticklers about third party libraries, many of them have already accepted apache commons. So, inclusion in this widely accepted library would make many people's lives much easier. A quick search on google reveals that most people either try going through a ByteBuffer, or try doing the bit shifts manually. There are also a large amount of people trying to do the manual bit shifts and coming up with errors, because they don't understand it well enough.

Here are just a few of the instances of people seeking the solution which this fix would provide.

Thank you, yall do great work.
-Yurelle
----

Byte Array to Short, and back:
https://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java
https://stackoverflow.com/questions/12199487/converting-from-byte-array-to-short-producing-wrong-negative-value
https://stackoverflow.com/questions/736815/2-bytes-to-short-java
https://stackoverflow.com/questions/2188660/convert-short-to-byte-in-java
https://stackoverflow.com/questions/8533607/short-to-byte-and-byte-to-short-conversion-in-android
http://forums.devshed.com/java-help-9/convert-bytes-short-580873.html


Byte Array to Int, and back:
https://stackoverflow.com/questions/7619058/convert-a-byte-array-to-integer-in-java-and-vise-versa
https://stackoverflow.com/questions/5616052/how-can-i-convert-a-4-byte-array-to-an-integer
https://stackoverflow.com/questions/5399798/byte-array-and-int-conversion-in-java
https://stackoverflow.com/questions/9581530/converting-from-byte-to-int-in-java
http://bethecoder.com/applications/articles/java/basics/how-to-convert-byte-array-to-integer.html
https://www.petefreitag.com/item/183.cfm
https://notinthemanual.blogspot.com/2008/05/convert-byte-array-to-integer-in-java.html

Byte Array to Long, and Back:
https://stackoverflow.com/questions/1026761/how-to-convert-a-byte-array-to-its-numeric-value-java
https://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java
https://stackoverflow.com/questions/1586882/how-do-i-convert-a-byte-to-a-long-in-java
https://stackoverflow.com/questions/36897582/is-there-a-way-to-convert-from-byte-to-long
https://coderanch.com/t/378647/java/convert-long-byte-array
https://coderanch.com/t/438753/java/long-byte-array


Byte Array to Float, and back:
https://stackoverflow.com/questions/13469681/how-to-convert-4-bytes-array-to-float-in-java
https://stackoverflow.com/questions/22250952/converting-byte-array-to-float
https://stackoverflow.com/questions/14619653/converting-a-float-to-a-byte-array-and-vice-versa-in-java
https://stackoverflow.com/questions/9346746/convert-float-to-byte-to-float-again
https://coderanch.com/t/410148/java/byte-float
http://www.thecodingforums.com/threads/convert-a-byte-array-to-a-float.129791/
http://forum.codecall.net/topic/76359-trasform-a-bytes-array-to-float-array-for-fft/


Byte Array to Double, and back:
https://stackoverflow.com/questions/2905556/how-can-i-convert-a-byte-array-into-a-double-and-back
https://stackoverflow.com/questions/32483958/how-to-convert-a-byte-array-to-double-array
https://stackoverflow.com/questions/20009472/how-to-convert-byte-array-to-double-in-java-without-using-bytebuffer
https://coderanch.com/t/545613/java/lightweight-convert-double-integers-byte


Byte Array to Hex String, and back:
https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
https://stackoverflow.com/questions/15429257/how-to-convert-byte-array-to-hexstring-in-java
https://stackoverflow.com/questions/332079/in-java-how-do-i-convert-a-byte-array-to-a-string-of-hex-digits-while-keeping-l
https://stackoverflow.com/questions/19450452/how-to-convert-byte-array-to-hex-format-in-java
https://coderanch.com/t/526487/java/Java-Byte-Hex-String

> Add number to byte array methods
> --------------------------------
>
>                 Key: LANG-341
>                 URL: https://issues.apache.org/jira/browse/LANG-341
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>            Reporter: Lilianne E. Blaze
>             Fix For: Discussion
>
>         Attachments: 341-v1-src.patch, 341-v1-test.patch, LANG-341-2.patch, LANG-341.patch
>
>
> Hello,
> I need a set of methods to convert Long to or from a byte[] array, as if
> writing / reading from Data(Input/Output)Stream(
> ByteArray(Input/Output)Stream ).
> First, doing it with Streams seems a bit wasteful, second, it seems a
> pretty general use. Would it be possible to add something like that to,
> for example,
> org.apache.commons.lang.math.NumberUtils?
> Example code:
> {code:java}
> static public long toLong(byte[] b)
>   {
>     return toLong(b, 0);
>   }
>   
>   static public long toLong(byte[] b, int offset)
>   {
>     return (((long)b[offset] << 56) +
>         ((long)(b[offset + 1] & 255) << 48) +
>         ((long)(b[offset + 2] & 255) << 40) +
>         ((long)(b[offset + 3] & 255) << 32) +
>         ((long)(b[offset + 4] & 255) << 24) +
>         ((b[offset + 5] & 255) << 16) +
>         ((b[offset + 6] & 255) <<  8) +
>         ((b[offset + 7] & 255) <<  0));
>   }
>   
>   static public byte[] longToByteArray(long l)
>   {
>     byte b[] = new byte[8];
>     longToByteArray(l, b, 0);
>     return b;
>   }
>   
>   static public void longToByteArray(long l, byte b[], int offset)
>   {
>     b[offset] = (byte)(l >>> 56);
>     b[offset + 1] = (byte)(l >>> 48);
>     b[offset + 2] = (byte)(l >>> 40);
>     b[offset + 3] = (byte)(l >>> 32);
>     b[offset + 4] = (byte)(l >>> 24);
>     b[offset + 5] = (byte)(l >>> 16);
>     b[offset + 6] = (byte)(l >>>  8);
>     b[offset + 7] = (byte)(l >>>  0);
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)