Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: bit package Date: Sat, 17 Feb 2018 16:23:38 +0100 Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net dG6obXa0KPI1/Ei7uOGopA257UtPP5rgyc3tnt2fwTWR0BbJg= Cancel-Lock: sha1:9niIHwKk87+0VRSkxn5xLGlycKc= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 In-Reply-To: Content-Language: en-GB Xref: csiph.com comp.lang.java.programmer:38030 On 17.02.2018 09:33, Philipp Kraus wrote: > Hello, > > 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? > Thanks You could use ASN.1 or Google's "Protocol Buffers". There are plenty of libs around: https://duckduckgo.com/?q=java+asn.1&ia=qa https://developers.google.com/protocol-buffers/ https://developers.google.com/protocol-buffers/docs/javatutorial Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/