Commons Lang
  1. Commons Lang
  2. LANG-801

Util for conversion between primitive types

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: lang.*
    • Labels:
      None

      Description

      A utility class to convert data from one primitive type to another, in the most commonly used fashions (big endian/little endian byte order, lsb first or msb first).
      It also deals with arrays of primitive types and String containing hexadecimal numbers.

      Such conversions are needed when dealing with embedded software or hardware modeling/design.
      I attach a initial version which deals with the "default" encoding, the one which seems the most natural on the JVM: little endian byte ordering, and, within a byte, lsb first.
      I plan to implement the other encoding I need:

      • big endian, lsb first
      • big endian, msb first

      So far I implemented about half of the methods needed to cover the default encoding:

      • methods to convert a large data type into an array of a smaller data type (longToBytes for example)
      • methods to convert an array of a small data type into a larger data type (bytesToLong for example)

      The remaining half consist of the methods to convert from an array of a data type into an array of another data type (bytesToLongs, LongsToBytes)
      Once this is available for different encodings, the most useful part can be done: the "bridge" methods to convert from one encoding to another

      1. patch_Conversion.txt
        143 kB
        Sebastien Riou

        Issue Links

          Activity

          Sebastien Riou created issue -
          Hide
          Sebastien Riou added a comment -

          the utility class and its unit test.

          Show
          Sebastien Riou added a comment - the utility class and its unit test.
          Sebastien Riou made changes -
          Field Original Value New Value
          Attachment patch_Conversion.txt [ 12525442 ]
          Sebastien Riou made changes -
          Description A utility class to convert data from one primitive type to another, in the most commonly used fashions (big endian/little endian byte order, lsb first or msb first).
          It also deals with arrays of primitive types and String containing hexadecimal numbers.

          Such conversions are needed when dealing with embedded software or hardware modeling/design.
          I attach a initial version which deals with the "default" encoding, the one which seems the most natural on the JVM: little endian byte ordering, and, within a byte, lsb first.
          I plan to implement the other encoding I need:
           - big endian, lsb first
           - big endian, msb first

          So far I implemented about half of the methods needed to cover the default encoding:
           - methods to convert a large data type into an array of a smaller data type (longToBytes for example)
           - methods to convert an array of a small data type into a larger data type (bytesToLong for example)

          The remaining half consist of the methods to convert from an array of a data type into an array of another data type (bytesToLongs, LongsToBytes)
          Once this is available for different encodings, the most useful part can be done: the "bridge" methods to convert from one encoding to another

          Warning: newbie speaking here! I don't see how to attach a file here. Since a guy from the mailing list suggested to "do a JIRA and attach the files", I assume it is possible to attach the files only after the issue is created, well, I will see...
          A utility class to convert data from one primitive type to another, in the most commonly used fashions (big endian/little endian byte order, lsb first or msb first).
          It also deals with arrays of primitive types and String containing hexadecimal numbers.

          Such conversions are needed when dealing with embedded software or hardware modeling/design.
          I attach a initial version which deals with the "default" encoding, the one which seems the most natural on the JVM: little endian byte ordering, and, within a byte, lsb first.
          I plan to implement the other encoding I need:
           - big endian, lsb first
           - big endian, msb first

          So far I implemented about half of the methods needed to cover the default encoding:
           - methods to convert a large data type into an array of a smaller data type (longToBytes for example)
           - methods to convert an array of a small data type into a larger data type (bytesToLong for example)

          The remaining half consist of the methods to convert from an array of a data type into an array of another data type (bytesToLongs, LongsToBytes)
          Once this is available for different encodings, the most useful part can be done: the "bridge" methods to convert from one encoding to another
          Joerg Schaible made changes -
          Assignee Joerg Schaible [ joehni ]
          Joerg Schaible made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          Hide
          Sebb added a comment -

          There aeems to be a lot of unnecessary conversion to/from Integer/Short etc. instead of sticking to primitive types.

          For example:

          Short bits = (short)(0xff & (src >> shift));
          dst[dstPos + i] = bits.byteValue();

          could be expressed as

          dst[dstPos + i] = (byte) (0xff & (src >> shift));

          etc.

          Show
          Sebb added a comment - There aeems to be a lot of unnecessary conversion to/from Integer/Short etc. instead of sticking to primitive types. For example: Short bits = (short)(0xff & (src >> shift)); dst [dstPos + i] = bits.byteValue(); could be expressed as dst [dstPos + i] = (byte) (0xff & (src >> shift)); etc.
          Hide
          Joerg Schaible added a comment -

          Actually I simply applied this for use with LANG-778. There's for sure room for improvement. However, it would be nice, if as long as the status is "In progress" only one of us would touch the files.

          Show
          Joerg Schaible added a comment - Actually I simply applied this for use with LANG-778 . There's for sure room for improvement. However, it would be nice, if as long as the status is "In progress" only one of us would touch the files.
          Joerg Schaible made changes -
          Status In Progress [ 3 ] Open [ 1 ]
          Hide
          Joerg Schaible added a comment -

          Removed a lot of useless conversions now.

          Should replace more with JDK functionality? E.g. conversion to hex can be done with Long.toHexString(long) - just that the resulting string has lowercase characters.

          Show
          Joerg Schaible added a comment - Removed a lot of useless conversions now. Should replace more with JDK functionality? E.g. conversion to hex can be done with Long.toHexString(long) - just that the resulting string has lowercase characters.
          Hide
          Sebastien Riou added a comment -

          It is true that a lot of methods are already available in standard java or in some other class of Commons* however the intention was to have one consistent API for those conversions (and the "casing" of string result would be part of the API contract).
          Sorry I have been unable to work more on this recently, and probably for few month more (changing continent and job, have to learn the local language...)

          Show
          Sebastien Riou added a comment - It is true that a lot of methods are already available in standard java or in some other class of Commons* however the intention was to have one consistent API for those conversions (and the "casing" of string result would be part of the API contract). Sorry I have been unable to work more on this recently, and probably for few month more (changing continent and job, have to learn the local language...)
          Joerg Schaible made changes -
          Link This issue blocks LANG-778 [ LANG-778 ]
          Hide
          Joerg Schaible added a comment -

          Added with minor adjustment to naming conventions.

          Show
          Joerg Schaible added a comment - Added with minor adjustment to naming conventions.
          Joerg Schaible made changes -
          Resolution Fixed [ 1 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Gavin made changes -
          Link This issue blocks LANG-778 [ LANG-778 ]
          Gavin made changes -
          Link This issue is depended upon by LANG-778 [ LANG-778 ]
          Henri Yandell made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open In Progress In Progress
          147d 4h 3m 1 Joerg Schaible 27/Sep/12 18:40
          In Progress In Progress Open Open
          4h 31m 1 Joerg Schaible 27/Sep/12 23:11
          Open Open Resolved Resolved
          6d 28m 1 Joerg Schaible 03/Oct/12 23:40
          Resolved Resolved Closed Closed
          376d 3h 53m 1 Henri Yandell 15/Oct/13 03:33

            People

            • Assignee:
              Joerg Schaible
              Reporter:
              Sebastien Riou
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development