473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2890
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robb Gilmore | last post by:
Hello, We have a C#.NET app which is calling a Java webservice. We use the wsdl file exportted from the java webservice to create our web-reference in Visual Studio. We are able to create the...
7
by: Johnathan Doe | last post by:
I can google search to find the range of values that can be represented in a float by reading up on the IEEE std, but is that the same as how many distinct values that can go in a float type? ...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
13
by: kennethlou | last post by:
Hi, If in C a variable appears like X=10.000000, I can round it to zero decimal places. ie X=10? I then have to save the number into a variable. The method appears below:...
5
by: Niall | last post by:
I'm experience problems very similar to those reported in this thread on dotnet.framework on May 16 at...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
2
by: Synapse | last post by:
hello guys.. i do really need some help here. i have to do the 4 basic mathematical operation by entering 2 numbers with decimal. example. 1st num: 12.50 2nd num: 6.5 the answer must be in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.