-2

I'm working on a Java project where I have been tasked with extracting raw data from both integer and long variables.

Here's an input:

Integer intValue = 42;
Long longValue = 9876543210L;

How can I extract the raw data as bytes from intValue and longValue?

Aakash Patel
  • 549
  • 5
  • 19
  • 4
    maybe it would be wise to ask the "GLI (Gaming Laboratories International) team" or whoever tasked you with extracting raw data – user16320675 May 26 '23 at 11:41
  • @user16320675 we are trying to findout the things from our end, so asked the question here – Aakash Patel May 26 '23 at 11:48
  • 1
    no sure what is meant by 'our end'... who better would know 'the exact meaning or requirements of "raw data" ' (my understanding is that an integer {or long} already is raw data... unless they are composed, like bit-masks or so) || anyway the question is unclear (maybe clear for someone involved with that GLI) – user16320675 May 26 '23 at 12:35
  • @user16320675 please check the answer, this is what i need i believe, but will clarify the answer with GLI team – Aakash Patel May 26 '23 at 12:39
  • Beats me why hex representation is raw data. Its more encoded than the variables. Nutty requirement. – Rohit Gupta Jun 23 '23 at 02:29

1 Answers1

2

For integer

byte[] bytes = ByteBuffer.allocate(Integer.BYTES).putInt(42).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

For long

byte[] bytes = ByteBuffer.allocate(Long.BYTES).putLong(9876543210L).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

The above prints in hexadecimal format. To print each byte as 0/1 style bits use:

   System.out.format(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
John Williams
  • 4,252
  • 2
  • 9
  • 18
  • `Integer.BYTES` and `Long.BYTES` provide the size of each type in bytes. And you have one too many opening parens after allocate. – WJS May 26 '23 at 12:10
  • 1
    @WJS Fixed the double parenthesis typo. But SIZE - The number of **bits** used to represent a long value in two's complement binary form. – John Williams May 26 '23 at 12:31
  • @JohnWilliams I see you are printing 0 0 0 0 0 0 0 1 in format of: 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x1 Can you please elaborate why it is being done in that format? – Aakash Patel May 26 '23 at 12:32
  • 0x indicates hexidecimal. What format do you prefer? binary? 01011010 – John Williams May 26 '23 at 12:36
  • *`But SIZE - The number of bits used to represent a long value in two's complement binary form.`* I know, and BYTES is but since you are computing the number of bytes in a long or int to allocate the byte array I just thought the existing predefined values would be clearer. Both work equally well. – WJS May 26 '23 at 12:39
  • @JohnWilliams can you please also tell how to represent it in binary? – Aakash Patel May 26 '23 at 12:41
  • 1
    @AakashPatel I have added to binary System.out.format to the answer. – John Williams May 26 '23 at 12:44
  • 1
    To print the original value in binary with padding you can do `String bits = Integer.toBinaryString(intValue);System.out.println("0".repeat(Integer.SIZE-bits.length()) + bits);` – WJS May 26 '23 at 12:58
  • 1
    Thank you @JohnWilliams this has been what i was looking for. My code for generating random numbers as per GLI standard is approved. – Aakash Patel Jun 20 '23 at 04:43
  • Thank you @WJS for providing valueable imrovements – Aakash Patel Jun 20 '23 at 04:44