commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vincent Ricard (Updated) (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (LANG-341) [NumberUtils] Please add number <> byte[] methods
Date Mon, 14 Nov 2011 13:56:51 GMT

     [ https://issues.apache.org/jira/browse/LANG-341?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Vincent Ricard updated LANG-341:
--------------------------------

    Attachment: LANG-341-2.patch

Here is a patch with conversion methods for: short, int, float, long and double (based on
the previous patch of Henri).

Should we check the buffer length? (in addittion to the null checking)

Should we add other methods with a default value? (toPrimitive(byte[] buff, primitive default))
                
> [NumberUtils] Please add number <> byte[] methods
> -------------------------------------------------
>
>                 Key: LANG-341
>                 URL: https://issues.apache.org/jira/browse/LANG-341
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.math.*
>            Reporter: Lilianne E. Blaze
>             Fix For: 3.x
>
>         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:
> 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);
>   }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message