Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38026 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| First post | 2018-02-17 00:33 -0800 |
| Last post | 2018-02-17 10:25 -0500 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2018-02-17 00:33 -0800 |
| Subject | bit package |
| Message-ID | <bebd1080-35be-485d-ac9d-f7e2d4b361da@googlegroups.com> |
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
Phil
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2018-02-17 07:14 -0500 |
| Message-ID | <p696af$tiv$1@dont-email.me> |
| In reply to | #38026 |
On 2/17/2018 3:33 AM, 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;
> ...
> }
Aside: This approach won't always work, not even in C. (Think
padding bytes, endianness, ...)
> 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?
It's not clear to me precisely what you're trying to do (the
semantics of l_object.set() are not obvious), but I think you might
find java.nio.ByteBuffer a good starting place.
--
esosman@comcast-dot-net.invalid
One thousand sixty-eight days to go.
[toc] | [prev] | [next] | [standalone]
| From | Kiwi User <martin@mydomain.invalid> |
|---|---|
| Date | 2018-02-17 12:40 +0000 |
| Message-ID | <p697s2$4go$1@news.albasani.net> |
| In reply to | #38027 |
On Sat, 17 Feb 2018 07:14:05 -0500, Eric Sosman wrote:
> On 2/17/2018 3:33 AM, 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;
>> ...
>> }
>
> Aside: This approach won't always work, not even in C. (Think
> padding bytes, endianness, ...)
>
>> 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?
>
> It's not clear to me precisely what you're trying to do (the
> semantics of l_object.set() are not obvious), but I think you might find
> java.nio.ByteBuffer a good starting place.
The BitSet class looks fairly straight forward and is explicitly little
endian.
I can't comment on BitObject because I'm using OpenJava 8, the current
version for RedHat Fedora 27. This doesn't provide a BitObject class.
--
Martin | martin at
Gregorie | gregorie dot org
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2018-02-17 08:16 -0500 |
| Message-ID | <p69a02$kg5$1@dont-email.me> |
| In reply to | #38028 |
On 2/17/2018 7:40 AM, Kiwi User wrote:
> On Sat, 17 Feb 2018 07:14:05 -0500, Eric Sosman wrote:
>
>> On 2/17/2018 3:33 AM, 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;
>>> ...
>>> }
>>
>> Aside: This approach won't always work, not even in C. (Think
>> padding bytes, endianness, ...)
>>
>>> 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?
>>
>> It's not clear to me precisely what you're trying to do (the
>> semantics of l_object.set() are not obvious), but I think you might find
>> java.nio.ByteBuffer a good starting place.
>
> The BitSet class looks fairly straight forward and is explicitly little
> endian.
Could you elaborate on what you mean by "explicitly little endian?"
I see no guarantee in the Javadoc about how the bits are stored, and
nothing at all about how one would marshal them for I/O.
> I can't comment on BitObject because I'm using OpenJava 8, the current
> version for RedHat Fedora 27. This doesn't provide a BitObject class.
There's no BitObject in Java 9, either. I imagine the O.P. simply
used a made-up class name to flesh out the example.
--
esosman@comcast-dot-net.invalid
One thousand sixty-eight days to go.
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2018-02-17 07:51 -0800 |
| Message-ID | <c08311e7-0ff3-46b8-823a-0b89b3b312ac@googlegroups.com> |
| In reply to | #38027 |
Am Samstag, 17. Februar 2018 13:14:22 UTC+1 schrieb Eric Sosman:
> On 2/17/2018 3:33 AM, 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;
> > ...
> > }
>
> Aside: This approach won't always work, not even in C. (Think
> padding bytes, endianness, ...)
>
> > 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?
>
> It's not clear to me precisely what you're trying to do (the
> semantics of l_object.set() are not obvious), but I think you might
> find java.nio.ByteBuffer a good starting place.
>
I'm searching something like Node.JS Buffer for Java
https://nodejs.org/api/buffer.html
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-02-17 11:11 -0500 |
| Message-ID | <p69k7h$1dcp$1@gioia.aioe.org> |
| In reply to | #38032 |
On 2/17/2018 10:51 AM, Philipp Kraus wrote: > Am Samstag, 17. Februar 2018 13:14:22 UTC+1 schrieb Eric Sosman: >> It's not clear to me precisely what you're trying to do (the >> semantics of l_object.set() are not obvious), but I think you might >> find java.nio.ByteBuffer a good starting place. >> > > I'm searching something like Node.JS Buffer for Java > https://nodejs.org/api/buffer.html That is java.nio.ByteBuffer. Arne PS: My Record library also use ByteBuffer below the surface.
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus.flashpixx@gmail.com> |
|---|---|
| Date | 2018-02-17 11:41 -0800 |
| Message-ID | <e9916c47-6e07-4db0-99bc-0f7bb9468536@googlegroups.com> |
| In reply to | #38033 |
Am Samstag, 17. Februar 2018 17:11:47 UTC+1 schrieb Arne Vajhøj: > On 2/17/2018 10:51 AM, Philipp Kraus wrote: > > Am Samstag, 17. Februar 2018 13:14:22 UTC+1 schrieb Eric Sosman: > >> It's not clear to me precisely what you're trying to do (the > >> semantics of l_object.set() are not obvious), but I think you might > >> find java.nio.ByteBuffer a good starting place. > >> > > > > I'm searching something like Node.JS Buffer for Java > > https://nodejs.org/api/buffer.html > > That is java.nio.ByteBuffer. > > Arne > > PS: My Record library also use ByteBuffer below the surface. Seems to be a great work. Did you publish this on Maven Central? I don't find it
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-02-17 14:55 -0500 |
| Message-ID | <p6a1bi$8as$1@gioia.aioe.org> |
| In reply to | #38036 |
On 2/17/2018 2:41 PM, Philipp Kraus wrote: > Am Samstag, 17. Februar 2018 17:11:47 UTC+1 schrieb Arne Vajhøj: >> PS: My Record library also use ByteBuffer below the surface. > > Seems to be a great work. Did you publish this on Maven Central? I don't find it No. I only support the old fashioned way - download and unzip. Arne
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2018-02-17 11:12 -0500 |
| Message-ID | <p69k92$po7$1@dont-email.me> |
| In reply to | #38032 |
On 2/17/2018 10:51 AM, Philipp Kraus wrote:
> Am Samstag, 17. Februar 2018 13:14:22 UTC+1 schrieb Eric Sosman:
>> On 2/17/2018 3:33 AM, 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;
>>> ...
>>> }
>>
>> Aside: This approach won't always work, not even in C. (Think
>> padding bytes, endianness, ...)
>>
>>> 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?
>>
>> It's not clear to me precisely what you're trying to do (the
>> semantics of l_object.set() are not obvious), but I think you might
>> find java.nio.ByteBuffer a good starting place.
>>
>
> I'm searching something like Node.JS Buffer for Java
> https://nodejs.org/api/buffer.html
... which documents no set() or get() methods. ;-)
In addition to java.nio.ByteBuffer, take a look at
java.io.DataOutput and the classes that implement it. These
latter will require additional effort if you must deal with
endianness issues, but the effort should be minimal.
--
esosman@comcast-dot-net.invalid
One thousand sixty-eight days to go.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-02-17 12:27 -0500 |
| Message-ID | <p69om9$1nkb$1@gioia.aioe.org> |
| In reply to | #38034 |
On 2/17/2018 11:12 AM, Eric Sosman wrote: > On 2/17/2018 10:51 AM, Philipp Kraus wrote: >> I'm searching something like Node.JS Buffer for Java >> https://nodejs.org/api/buffer.html > > ... which documents no set() or get() methods. ;-) But has write and read method ... Arne
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Date | 2018-02-17 20:55 +0100 |
| Message-ID | <p6a1cr$okp$1@dont-email.me> |
| In reply to | #38032 |
On 2018-02-17 16:51, Philipp Kraus wrote:
> Am Samstag, 17. Februar 2018 13:14:22 UTC+1 schrieb Eric Sosman:
>> On 2/17/2018 3:33 AM, 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;
>>> ...
>>> }
>>
>> Aside: This approach won't always work, not even in C. (Think
>> padding bytes, endianness, ...)
>>
>>> 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?
>>
>> It's not clear to me precisely what you're trying to do (the
>> semantics of l_object.set() are not obvious), but I think you might
>> find java.nio.ByteBuffer a good starting place.
>>
>
> I'm searching something like Node.JS Buffer for Java
> https://nodejs.org/api/buffer.html
>
Don't use a BitSet for this; it's not really what it's for or what it
represents -- a set of bits. As Eric says, the closest equivalent will
be java.nio.Buffer and its variants, particularly ByteBuffer.
There are no unsigned primitive types (char aside, where it's likely
best left be for your purposes) in Java, so you'll have to mask around, e.g.
uint8 -> 1 byte -> Java byte type: int uint8 = uint8_byte & 0xff,
uint16 -> 2 bytes -> Java short type: int uint16 = uint16_short & 0xffff,
and so forth. Note that you will not be able to represent an unsigned
64-bit integer in Java using a primitive; you'll have to use BigInteger.
Arne's lib may be a nice and easy alternative. There should be some
ASN.1 and DER libs around as well, if applicable.
--
DF.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2018-02-17 16:23 +0100 |
| Message-ID | <feqvnrF387nU1@mid.individual.net> |
| In reply to | #38026 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-02-17 10:25 -0500 |
| Message-ID | <p69hgm$191u$1@gioia.aioe.org> |
| In reply to | #38026 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web