reconstruct a float number in c# that was received from a java ser

G

Guest

Hi all,

i need to read a float number in my c# client that is received from a socket
from a java server.

1. i think i found a sollution, here it is :

public float readFloat()
{
return BitConverter.ToSingle(BitConverter.GetBytes(readInt()),0);
}

it seems ok,
is it ok ? (readInt() is my method that read a 4 bytes as int from a socket
in a BigEndian order)

2. but when i use instead the BitConverter class the Convert class like this :
return Convert.ToSingle(readInt());
it doesn't work ok? what's wrong ?

Thanks.
 
J

Jon Skeet [C# MVP]

yaron said:
i need to read a float number in my c# client that is received from a socket
from a java server.

1. i think i found a sollution, here it is :

public float readFloat()
{
return BitConverter.ToSingle(BitConverter.GetBytes(readInt()),0);
}

it seems ok,
is it ok ? (readInt() is my method that read a 4 bytes as int from a socket
in a BigEndian order)

I would suggest using my BinaryReader/BitConverter equivalents that
allow you to specify the endianness. See
http://www.pobox.com/~skeet/csharp/miscutil
2. but when i use instead the BitConverter class the Convert class like this :
return Convert.ToSingle(readInt());
it doesn't work ok? what's wrong ?

Well, Convert.ToSingle(int) just converts an integer to the nearest
float. It's not doing the same thing as BitConverter at all.
 
G

Guest

Hi Jon,

you already help me before with volatile field.
i start look on your misc dll it have very cool classes that can help me.

Thanks again Jon.
 
J

Jon Skeet [C# MVP]

yaron said:
you already help me before with volatile field.
i start look on your misc dll it have very cool classes that can help me.

If you like what you see now, you might want to check it again in a few
days. I'm modifying the SyncLock code to allow you to specify the
ordering of locks. Hopefully I'll have it done by Christmas...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top