Advertisement
hhac

Wrap Example

May 26th, 2021
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.nio.charset.StandardCharsets;
  2. import java.nio.charset.Charset;
  3. import java.nio.ByteBuffer;
  4.  
  5. public class WrapExample {
  6.  
  7.      public static void main(String []args) {
  8.          
  9.          byte[] data = "HECTOR".getBytes(StandardCharsets.UTF_8);
  10.          
  11.          printBytesValues(data);
  12.          
  13.          ByteBuffer buffer = ByteBuffer.wrap(data);
  14.          
  15.          printBytesValues(buffer.array());
  16.          
  17.          buffer.position(0);
  18.          
  19.          int aIntValue = buffer.getInt();
  20.          
  21.          System.out.println(String.format("0x%02X", aIntValue));
  22.          
  23.          if (aIntValue == 0x48454354) {
  24.              System.out.println("This is good");
  25.          } else {
  26.              System.out.println("This is not good");
  27.          }
  28.          
  29.          buffer.position(1);
  30.          
  31.          aIntValue = buffer.getInt();
  32.          
  33.          System.out.println(String.format("0x%02X", aIntValue));
  34.          
  35.          if (aIntValue == 0x4543544F) {
  36.              System.out.println("This is good");
  37.          } else {
  38.              System.out.println("This is not good");
  39.          }
  40.      }
  41.      
  42.      static void printBytesValues(byte[] data) {
  43.        
  44.          System.out.print("Data: ");
  45.          for (int i = 0; i < data.length; i ++) {
  46.               System.out.print(String.format("[0x%02X]", data[i]));
  47.          }
  48.          System.out.print("\n");
  49.      }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement