471,605 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

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

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(readIn t()),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.
Dec 21 '05 #1
3 2766
yaron <ya***@discussions.microsoft.com> wrote:
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(readIn t()),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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #2
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.
"Jon Skeet [C# MVP]" wrote:
yaron <ya***@discussions.microsoft.com> wrote:
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(readIn t()),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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 21 '05 #3
yaron <ya***@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robb Gilmore | last post: by
7 posts views Thread by Johnathan Doe | last post: by
13 posts views Thread by kennethlou | last post: by
16 posts views Thread by chandanlinster | last post: by
45 posts views Thread by Carramba | last post: by
2 posts views Thread by Synapse | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.