Skip to content

Implicit conversion from integral numbers to byte array #2702

@scordio

Description

@scordio

I'd like to propose the addition of implicit conversion from integral numbers (byte, short, int, long) to byte array. The concrete use case is code dealing with bitwise operations, mostly related to cryptography.

My wish is to write something like this:

  @ParameterizedTest
  @ValueSource(ints = {Integer.MIN_VALUE, 0, 0xFF, 0xFFFF, 0xFFFFFF, Integer.MAX_VALUE})
  void test(byte[] byteArray) {
    assertThat(byteArray).hasSize(4);
  }

where I can leverage binary and hexadecimal literals which are automatically converted to a byte array using a ByteBuffer with Big Endian order.

I put together a small poc with a custom argument converter. If the proposal is accepted, I'm happy to raise a PR about it.

Deliverables

  • A new implicit conversion from byte / short / int / long to byte[]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions