Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38031

Re: bit package

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: bit package
Date 2018-02-17 10:25 -0500
Organization Aioe.org NNTP Server
Message-ID <p69hgm$191u$1@gioia.aioe.org> (permalink)
References <bebd1080-35be-485d-ac9d-f7e2d4b361da@googlegroups.com>

Show all headers | View raw


On 2/17/2018 3:33 AM, Philipp Kraus wrote:
> I'm building a network communication protocol for hardware-communication, so I need a bit representation of my dara like a C struct:
> 
> struct
> {
>      unint8 field1;
>      unint16 field2;
> ...
> }
> 
> On my Java classes I have got int, long, ... so how can I create such bit structures?
> My first try is to use a BitSet and convert all datatypes manually, but is there a better possibility to create such bit sequences. I need a sending and receiving structure. I'm working with Java 9.
> 
> I the best case I create an object like
> 
> BitObject l_obj = new BitObject( 8, 16, 8, 32 );
> l_object.set(0, mybyte)
> l_object.set(1, myshort)
> l_object.set(2, mybyte)
> l_object.set(3,  myint)
> 
> and on reseiving
> 
> BitObject l_obj = new BitObject( mybytearray_from_socket, 8, 16, 8, 32 );
> l_obj.get(0)
> l_obj.get(1)
> ...
> 
> Can you suggest a library or anything for doing this?

I have created a little Record library for stuff like this.

Example:

import dk.vajhoej.record.Alignment;
import dk.vajhoej.record.Endian;
import dk.vajhoej.record.FieldType;
import dk.vajhoej.record.RecordException;
import dk.vajhoej.record.Struct;
import dk.vajhoej.record.StructField;
import dk.vajhoej.record.StructReader;
import dk.vajhoej.record.StructWriter;

public class StructSer {
     @Struct(alignment=Alignment.PACKED, endianess=Endian.LITTLE)
     public static class YourStruct {
         @StructField(n=0, type=FieldType.UINT1)
         private int field1;
         @StructField(n=1, type=FieldType.UINT2)
         private int field2;
         public YourStruct() {
             this(0, 0);
         }
         public YourStruct(int field1, int field2) {
             this.field1 = field1;
             this.field2 = field2;
         }
         public int getField1() {
             return field1;
         }
         public int getField2() {
             return field2;
         }
     }
     public static void main(String[] args) throws RecordException {
         YourStruct o = new YourStruct(2, 257);
         StructWriter sw = new StructWriter();
         sw.write(o);
         byte[] b = sw.getBytes();
         for(byte b1 : b) {
             System.out.println(b1);
         }
         StructReader sr = new StructReader(b);
         YourStruct o2 = sr.read(YourStruct.class);
         System.out.println(o2.getField1() + " " + o2.getField2());
     }
}

Output:

2
1
1
2 257

Link:

http://www.vajhoej.dk/arne/opensource/record/

(Apache license)

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

bit package Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2018-02-17 00:33 -0800
  Re: bit package Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-02-17 07:14 -0500
    Re: bit package Kiwi User <martin@mydomain.invalid> - 2018-02-17 12:40 +0000
      Re: bit package Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-02-17 08:16 -0500
    Re: bit package Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2018-02-17 07:51 -0800
      Re: bit package Arne Vajhøj <arne@vajhoej.dk> - 2018-02-17 11:11 -0500
        Re: bit package Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2018-02-17 11:41 -0800
          Re: bit package Arne Vajhøj <arne@vajhoej.dk> - 2018-02-17 14:55 -0500
      Re: bit package Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-02-17 11:12 -0500
        Re: bit package Arne Vajhøj <arne@vajhoej.dk> - 2018-02-17 12:27 -0500
      Re: bit package Daniele Futtorovic <da.futt.news@laposte.net> - 2018-02-17 20:55 +0100
  Re: bit package Robert Klemme <shortcutter@googlemail.com> - 2018-02-17 16:23 +0100
  Re: bit package Arne Vajhøj <arne@vajhoej.dk> - 2018-02-17 10:25 -0500

csiph-web