Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Need help to create byte array

Reply
Thread Tools

Need help to create byte array

 
 
ami
Guest
Posts: n/a
 
      11-02-2005
I think you are right I need to send bits over the socket. I can not
use objects I need to send bits. but I thought you can not have bits in
java that is why I am saying byte.
Please help regarding how to send bits?How to convert string or int
type in bits?

 
Reply With Quote
 
 
 
 
zero
Guest
Posts: n/a
 
      11-02-2005
"ami" <(E-Mail Removed)> wrote in news:1130973038.505110.107370
@f14g2000cwb.googlegroups.com:

> I think you are right I need to send bits over the socket. I can not
> use objects I need to send bits.


Why? An individual bit has no meaning, it must be part of some data, so
why would you need to send the individual bits?

> but I thought you can not have bits in
> java that is why I am saying byte.
> Please help regarding how to send bits?How to convert string or int
> type in bits?


Strings and ints *are* bits, just grouped together in a meaningful way.
Anyway, this may help:

Socket aSocket;
....
OutputStream output = aSocket.getOutputStream();
String sData = "hello world";
output.write(sData.bytes());
....

Don't forget to close your streams and sockets.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On 2 Nov 2005 09:30:14 -0800, "ami" <(E-Mail Removed)> wrote, quoted
or indirectly quoted someone who said :

>Can any one help and explain
>me how to create byte array for 1 and 0?


You could use a java.util.BitSet
or
byte[] bits = new byte[56];

There is no way you are going to get that preamble sent over the wire
in a way that fools other hardware into thinking it was hardware-
generated. All you can do is cart your bits around as data inside the
ethernet packets.

You are born a little to late. Back in the early 80s I lead a
Saturday morning Apple ][ club. We bought Hitachi X25 chips and build
ourselves a little LAN from the ground up. But even back then that
framing was all hardware-generated..



--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On Wed, 02 Nov 2005 19:15:56 +0100, Stefan Schulz <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Clearly java.lang.reflect.Array is the way to go. Look up the
>newInstance method (using byte.class as first argument)


he is just teasing.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On 2 Nov 2005 12:00:59 -0800, "ami" <(E-Mail Removed)> wrote, quoted
or indirectly quoted someone who said :

>How do I insert 10101010 for
>first byte in byte array? If possible can you give some code?


You then are putting 8 bits per byte.

so if you want to put

01010110 that is 0x56

b[i] = (byte) 0x56;

See http://mindprod.com/jgloss/unsigned.html

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On Wed, 02 Nov 2005 22:52:28 GMT, zero <(E-Mail Removed)> wrote, quoted or
indirectly quoted someone who said :

>> I need to create frame

>
>What do you mean by frame?


He is talking about an Ethernet frame, a hardware packet with some
preamble sync bits, addressing header.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On 2 Nov 2005 15:10:38 -0800, "ami" <(E-Mail Removed)> wrote, quoted
or indirectly quoted someone who said :

>I think you are right I need to send bits over the socket. I can not
>use objects I need to send bits. but I thought you can not have bits in
>java that is why I am saying byte.
>Please help regarding how to send bits?How to convert string or int
>type in bits?


I keep jumping about in my understanding what you want to accomplish.

1. If you want to do a software simulation of a small ethernet LAN all
running in one machine, that sounds like a doable project.

2. If you somehow think you are going to create ethernet frames and
shoot them out over your LAN forget it.

3. If you are trying to figure out how to make remote machines
communicate simulating an Ethernet LAN, then the preamble bits are
irrelevant. Further, there is nothing like the broadcast ability for
remotes and even if there were, it would be an inefficient way to
communicate.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On Wed, 02 Nov 2005 23:26:07 GMT, zero <(E-Mail Removed)> wrote, quoted or
indirectly quoted someone who said :

>> I think you are right I need to send bits over the socket. I can not
>> use objects I need to send bits.

>
>Why? An individual bit has no meaning, it must be part of some data, so
>why would you need to send the individual bits?


what he might mean is he wants to ability to control bits individually
in his messages. He needs a way of doing bit addressibility to get at
any bit.

See http://mindprod.com/jgloss/masking.html
http://mindprod.com/jgloss/binary.html

for tips on how to do bit twiddling.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-03-2005
On 2 Nov 2005 13:15:52 -0800, "ami" <(E-Mail Removed)> wrote, quoted
or indirectly quoted someone who said :

>Sorry If I am asking silly question but if I put -86 How do I convert
>it into 10101010 while reading it back.


Inside the machine everything is binary. We convert it to strings so
binary-challenged humans can make sense of it. You can also convert a
binary int to String of the letters '0' and '1' just as easily as the
String of the letters '0' '1' ... '9'.

See http://mindprod.com/jgloss/binary.html
for how.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Jack
Guest
Posts: n/a
 
      11-03-2005
On Thu, 03 Nov 2005 01:53:32 GMT, Roedy Green
<(E-Mail Removed) > wrote:

>>Can any one help and explain
>>me how to create byte array for 1 and 0?


geepers, I think all the guy wanted was to know how to specify to the
compiler that he is using binary notation

once upon a time, it was what? %100 meant 4, and not 100 decimal? You
didn't need to specify all eight bits, either. Am I remembering that
correctly?

So he wanted: byte[] array = new byte[%10101010, %10101010, ....]

But I believe that we can't do that in Java, as Roedy's glossary says.
So therefore, the next answer is that you can use hexadecimal
notation, 0xAA (which is somewhat more obvious than using decimal).

Or maybe it's 0x55 that he wants...


>
>You could use a java.util.BitSet
>or
>byte[] bits = new byte[56];


not to nitpick, but it'd be new byte[0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA] for 56 bits.

BUT, as Roedy says below, it's all moot - since you can't do what he
wants. You can oniy send TCP or UDP in Java, and maybe lately a bit of
ICMP.

>
>There is no way you are going to get that preamble sent over the wire
>in a way that fools other hardware into thinking it was hardware-
>generated. All you can do is cart your bits around as data inside the
>ethernet packets.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
Re: how to extend a byte[] array with a null byte? Tom McGlynn Java 4 04-18-2008 11:49 PM
Re: how to extend a byte[] array with a null byte? Patricia Shanahan Java 0 04-17-2008 06:47 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments