• Post Reply Bookmark Topic Watch Topic
  • New Topic

Receiving a C# float

 
Rob Grubb
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive just started socket programming and am trying to accept a float sent from a c# application using a java ByteBuffer.

My problem is that the number being sent from c# is 12.233f but when i receive it in java it ends up being 6.7108214E18.

I think it has something to do with Endianess but i don't know how to solve it. Please help.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

unfortunately I'm not familiar with C# so I don't know the details about the internal representation of data types in contrast to Java.

One way to solve this issues would be to try to understand what's happening here and to handle the differences between OSes, platforms and programming languages. But the easiest way to avoid problems which are related platform and/or language specific issues is probably to avoid to handle them :-) That means you could rely on some higher level transport format like XML for example. Or you could send the data as strings (which of course involves converting from and to strings on both sides).

Without knowing what exactly you are doing (and without knowing C#) it's hard to give realy helpful advices. Depending on your requirements and needs there was probably already someone other who has solved your problem before by providing some kind of framework or library which takes care of these low-level details you mentioned.

Marco
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Grubb wrote:I think it has something to do with Endianess but i don't know how to solve it. Please help.

ByteBuffer.order(ByteOrder) should help you out.
 
Rob Grubb
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys. put me in the right direction. This was the fix in the end. Single = float

 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's C# code, isn't it? Because I don't recognize any of these classes. (Well, there is Array, but I doubt that is java.lang.reflect.Array.)
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, a better solution is to pass string representations rather than binary. Just send the result of a printf on the C++ side, and then parse the string on the Java side.

Some will claim that the translation from binary to string and back is a waste of computer cycles. I claim that the overhead is trivial, far less than it takes to do all the code to send even a long string over a socket connection, and the clarity of the code, and ease of debugging makes up for it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!