• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion creating String  RSS feed

 
Nick Tons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, I'm working on a P2P simulation, and I need to set up messages to be sent from one peer to another. These messages need to be formatted in a certain way, such that bytes 0-15 of the message represent one field, byte 16 another field, etc. These messages must be sent as Strings.

I'm really lost on how I should be constructing these strings. For example, one field is essentially just a randomly generated number that goes in bytes 0-15...I have no problem generating the value that should be in the field, but as far as getting it into bytes 0-15 of a message I have no idea.

After constructing and sending the string, it then must be picked apart by the peer receiving the message...this would be pretty easy to do using bitmasks, but again, I am really lost as to how to set those up in Java.

To make matters worse, the fact that some of these fields need to be little-endian and some of them big-endian is really screwing with my head...I dont know if my data is going to be in the right order even!

Any advice or nudges in the right direction would be GREATLY appreciated.

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick,

Welcome to JavaRanch!

We probably need to talk about this a little to determine what your true requirements are. First of all, note that Java Strings are made up of "wide characters" -- 16 bits, not 8-bit bytes like C strings. And of course, those characters aren't little or big-endian -- they just are.

You probably want to assemble your data in an array of bytes -- i.e., a byte[]. This will let you very easily make assignments to the individual bytes within a message. If need be, you can also use bit masks on the bytes themselves.

Then, if necessary, you could encode that byte array for transmission as a String, but that encoding will probably have to be non-standard, something you come up with yourself, as converting arbitrary binary data directly to a String will leave you no special characters for delimiting the I/O (i.e., you might end up with spurious newlines apparing in your data, for example.)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"These messages must be sent as Strings" - I don't understand this statement. What do you mean by it?
 
Nick Tons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'll try to explain things a little better. I've made some progress on this with some help from a friend, but I am still struggling.

We have software in place already that handles the sending of messages...the methods that do this take a String as a parameter, that's what I meant by they must be sent as strings.

The message protocol I'm trying to implement is set up so that a certain range of bytes in the message represents one field. For example in the message header, bytes 0-15 are a randomly generated message ID, so basically I need to create a 16 byte long random number there. The next byte signifies the payload type, the next byte is an integer value representing the messages "time to live".

I think I can handle converting these byte arrays to strings and then from strings back to byte arrays now, but the current problem is actually constructing the byte arrays. I got some advice on another forum that the best way to do this might be something like this:



Which is what I'm planning to try...but currently I'm having issues constructing the fields. I am really confused how to set a byte[] "equal to" something. For example the 16 byte message ID needs to have 0xFF and 0x00 in bytes 8 and 15 respecitvely, with random values in the other bytes. I understand how I can do this with bit-masking, but JAVA doesn't allow me to use literal hexadecimal values, so it's proving a bit challenging.

Any advice you guy have for me would be greatly appreciated. I hope I haven't confused things more...please ask me questions if you need more info!!
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<hr></blockquote>

Do you know the size of your message? If it is true I think it would be better to create simple byte array.


I understand how I can do this with bit-masking, but JAVA doesn't allow me to use literal hexadecimal values, so it's proving a bit challenging.


I am not sure I get you right, but how about this and this?
[ June 29, 2006: Message edited by: Georgy Bolyuba ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use literal hexadecimal values.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!