🗒️
notes
  • Journal
  • URLs
  • Java Card
    • SCP02
    • Rapid Notes
    • _FIXVALS_
    • Mifare
    • Chain Of Trust
  • Encoding
    • CBEFF
    • Bytes
  • Snippets
    • JNI_OnLoad
  • float to byte[]
  • Protobuf
  • C/C++
    • Containers
    • Basics
    • JNI
    • gcov
    • Castings
  • chess
    • Untitled
  • Compression
    • Untitled
  • Snippets
    • Untitled
  • Build Systems
    • Maven
    • Windows
  • Gradle
  • CMake
  • Java
    • Untitled
    • Certificates
  • Android
    • Mifare
  • Python
    • ctypes
  • WebSub
    • References
  • Spring Boot
    • Form-based Authentication
    • Basic Access Authentication
    • JWT Authentication
  • QR Code
    • Denso QR Code
  • Philosophical Inquiry
    • First
  • XML
    • xmlstarlet
Powered by GitBook
On this page

Was this helpful?

float to byte[]

Reverse a byte[] in Java:

public static byte[] reverse(byte[] arr)
{
    byte[] buf = new byte[arr.length];
    int len = arr.length;
    for (int i = 0; i < len; i++) {
        buf[i] = arr[len - 1 - i];
    }
    return buf;
}

Java float -> byte[] :

  public static byte[] floatToByteArray(float value) 
  {
      int intBits =  Float.floatToIntBits(value);
      return new byte[] {
          (byte) (intBits >> 24), 
          (byte) (intBits >> 16), 
          (byte) (intBits >> 8), 
          (byte) (intBits) 
      };
  }

A more elegant way one-liner to do float -> byte[] :

ByteBuffer.allocate(4).putFloat(value).array();

Java byte[] -> float

  byte b[] = new byte[4]; 
  ByteBuffer buffer = ByteBuffer.wrap(b);
  float fval = buffer.getFloat();

PreviousJNI_OnLoadNextProtobuf

Last updated 4 years ago

Was this helpful?