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

          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.
          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...)
          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
          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.
          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
          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.

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development