![]() |
creating byte[] with subfields
I often have to construct byte arrays with binary fields, or read
them. I use a ByteArrayOutputStream and a DataOutputStream to compose them. I wondered if there is a simpler way, one that does not require computing each byte individually with shifts. -- Roedy Green Canadian Mind Products http://mindprod.com The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ~ Tom Cargill Ninety-ninety Law |
Re: creating byte[] with subfields
Roedy Green <see_website@mindprod.com.invalid> writes:
> I often have to construct byte arrays with binary fields, or read > them. > > I use a ByteArrayOutputStream and a DataOutputStream to compose them. > I wondered if there is a simpler way, one that does not require > computing each byte individually with shifts. In C, sure. Java by design doesn't allow those kinds of tricks. -- Jim Janney |
Re: creating byte[] with subfields
On 18/01/13 08:35, Roedy Green wrote:
> I often have to construct byte arrays with binary fields, or read > them. > > I use a ByteArrayOutputStream and a DataOutputStream to compose them. > I wondered if there is a simpler way, one that does not require > computing each byte individually with shifts. > BitSet, and its toByteArray() method may do what you require. I've not tried it myself. I still do it by shift/mask. -- Nigel Wade |
Re: creating byte[] with subfields
Jim Janney <jjanney@shell.xmission.com> writes:
> Roedy Green <see_website@mindprod.com.invalid> writes: > >> I often have to construct byte arrays with binary fields, or read >> them. >> >> I use a ByteArrayOutputStream and a DataOutputStream to compose them. >> I wondered if there is a simpler way, one that does not require >> computing each byte individually with shifts. > > In C, sure. Java by design doesn't allow those kinds of tricks. The hotspot compiler might notice that you're shifting and masking by 8 bits and replace it with direct byte addressing. That seems like a straightforward optimization. OTOH, the speed difference is probably too tiny to notice anyway. -- Jim Janney |
Re: creating byte[] with subfields
On 1/18/13 12:35 AM, Roedy Green wrote:
> I often have to construct byte arrays with binary fields, or read > them. > > I use a ByteArrayOutputStream and a DataOutputStream to compose them. > I wondered if there is a simpler way, one that does not require > computing each byte individually with shifts. > How about using a BitSet instead? Or, avoid this level of low-level work? Or, create a helper wrapper class. BitStream, where you can write a number of bits, rather than whole bytes. |
Re: creating byte[] with subfields
On Fri, 18 Jan 2013 10:53:34 -0800, Daniel Pitts
<newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly quoted someone who said : >How about using a BitSet instead? I don't often have to do anything finer than the byte. On project I did recently involved XOR encryption, where you need everything in bytes to be able to xor. Last night I was composing/decomposing a DataPacket for SingleInstance. I did one to handle Network Time Protocol. -- Roedy Green Canadian Mind Products http://mindprod.com The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ~ Tom Cargill Ninety-ninety Law |
Re: creating byte[] with subfields
On 1/18/13 2:23 PM, Roedy Green wrote:
> On Fri, 18 Jan 2013 10:53:34 -0800, Daniel Pitts > <newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly > quoted someone who said : > >> How about using a BitSet instead? > > I don't often have to do anything finer than the byte. > On project I did recently involved XOR encryption, where you need > everything in bytes to be able to xor. > > Last night I was composing/decomposing a DataPacket for > SingleInstance. > > I did one to handle Network Time Protocol. > FWIW, XOR encryption is more like XOR obfuscation. It doesn't actually encrypt, at least not the naive approach to it. |
Re: creating byte[] with subfields
On 1/18/2013 3:35 AM, Roedy Green wrote:
> I often have to construct byte arrays with binary fields, or read > them. > > I use a ByteArrayOutputStream and a DataOutputStream to compose them. > I wondered if there is a simpler way, one that does not require > computing each byte individually with shifts. A DataOutputStream on top of a ByteArrayOutputStream is the old Java way of doing it. And if you do not need to manipulate bits and you are happy with big endian then you should not to use shifts. The new Java way is ByteBuffer which support both big and little endian, so you only need shift for bit manipulation. Those are both pretty low level. If you need something more high level, then you will need to look outside of standard Java API. Arne PS: I actually have something on the shelf for that. |
Re: creating byte[] with subfields
On 1/18/2013 8:33 PM, Arne Vajhøj wrote:
> On 1/18/2013 3:35 AM, Roedy Green wrote: >> I often have to construct byte arrays with binary fields, or read >> them. >> >> I use a ByteArrayOutputStream and a DataOutputStream to compose them. >> I wondered if there is a simpler way, one that does not require >> computing each byte individually with shifts. > > A DataOutputStream on top of a ByteArrayOutputStream is the > old Java way of doing it. And if you do not need to manipulate bits > and you are happy with big endian then you should not to use shifts. > > The new Java way is ByteBuffer which support both big and little > endian, so you only need shift for bit manipulation. > > Those are both pretty low level. If you need something more > high level, then you will need to look outside of standard > Java API. > PS: I actually have something on the shelf for that. Data class: import dk.vajhoej.record.Endian; import dk.vajhoej.record.FieldType; import dk.vajhoej.record.Struct; import dk.vajhoej.record.StructField; @Struct(endianess=Endian.LITTLE) public class Data { @StructField(n=0,type=FieldType.BIT,length=4) private int bf1; @StructField(n=1,type=FieldType.BIT,length=4) private int bf2; @StructField(n=2,type=FieldType.INT4) private int iv; @StructField(n=3,type=FieldType.VARSTR,prefixlengt h=1,encoding="ISO-8859-1") private String sv; public Data() { this(0, 0, 0, ""); } public Data(int bf1, int bf2, int iv, String sv) { this.bf1 = bf1; this.bf2 = bf2; this.iv = iv; this.sv = sv; } public int getBf1() { return bf1; } public void setBf1(int bf1) { this.bf1 = bf1; } public int getBf2() { return bf2; } public void setBf2(int bf2) { this.bf2 = bf2; } public int getIv() { return iv; } public void setIv(int iv) { this.iv = iv; } public String getSv() { return sv; } public void setSv(String sv) { this.sv = sv; } } Test program: import dk.vajhoej.record.RecordException; import dk.vajhoej.record.StructReader; import dk.vajhoej.record.StructWriter; public class RW { public static void main(String[] args) throws RecordException { Data o1 = new Data(1, 2, 3, "ABC"); StructWriter sw = new StructWriter(); sw.write(o1); byte[] ba = sw.getBytes(); for(byte b1 : ba) { System.out.printf(" %02X" , b1); } System.out.println(); StructReader sr = new StructReader(ba); Data o2 = sr.read(Data.class); System.out.println(o2.getBf1() + " " + o2.getBf2() + " " + o2.getIv() + " " + o2.getSv()); } } Output: 12 03 00 00 00 03 41 42 43 1 2 3 ABC Arne |
Re: creating byte[] with subfields
On Fri, 18 Jan 2013 15:16:45 -0800, Daniel Pitts
<newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly quoted someone who said : >FWIW, XOR encryption is more like XOR obfuscation. It doesn't actually >encrypt, at least not the naive approach to it. It depends. I just finished a project which I will be permitted to release into the public domain in a year which uses one time pads, which XOR messages with strings of truly random bits. The only way you can crack it is to crack the machine at either end or intercept the key transfer. You can't do it just by studying messages. -- Roedy Green Canadian Mind Products http://mindprod.com The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ~ Tom Cargill Ninety-ninety Law |
| All times are GMT. The time now is 10:45 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.