0

I am working on a network related project wherein I need to transfer a structure data containing float variables. I know I can convert int and short variables to network byte order using htons and htonl functions, but are there any inbuilt functions for converting float and double variables to network byte order? Can somebody help me how to do this.

    struct Employee
    {
         float EmployeeSalary;
         unsigned short EmployeeId;
         ...
    };

int main()
{
    struct Employee emp;
    emp.EmployeeSalary=1234.567;
    emp.EmployeeId=123;

    struct Employee TempEmp;
    TempEmp.EmployeeSalary=?;
    TempEmp.EmployeeId=htons(emp.EmployeeId);

    char buf[100];
    memcpy(buf,Tempemp,sizeof(Employee));

    //send the buf data via sendto function
}

Also can somebody explain me how to do the same conversions for structs containing bit field data.

Rama
  • 3,222
  • 2
  • 11
  • 26
Harry
  • 2,177
  • 1
  • 19
  • 33
  • Why would you need that? You only have to convert numbers that are relevant for the network, like port numbers. – alain Mar 02 '17 at 15:06
  • I strongly recommend taking a look into Protobufs or similar frameworks – Marco A. Mar 02 '17 at 15:09
  • 1
    To "transfer ... float variables", involves more than endian issues. Different platforms may employ different range and precision. An alternative is to send FP values as _text_. See [Printf width specifier to maintain precision of floating-point value](http://stackoverflow.com/q/16839658/2410359) to do so and not lose information. – chux - Reinstate Monica Mar 02 '17 at 15:17
  • @PaulR That's not a dup of *swapping “endianness” of floats and doubles*. I't is about a portability problem. Well, it turns out that not all architectures represent a float(or int for that matter) with the same bit representation or even the same byte ordering! – Rama Mar 02 '17 at 15:39
  • @Rama: the flagged duplicate answers the OP's question *as posed*, i.e. the OP wants to reverse the byte ordering of floats and doubles, just like they are already doing for ints and shorts. Issues of data representation other than byte ordering are not mentioned in the question. – Paul R Mar 02 '17 at 16:05
  • @PaulR *"the OP wants to **reverse** the byte ordering of floats and doubles"*. That is your asumption, the OP never say that. He is asking for converter method, from host to network. – Rama Mar 02 '17 at 17:37
  • @Rama: right - and he's using [`htons`](https://linux.die.net/man/3/htons) as an example of what he wants to do - this just reverses the byte order (or not), depending on your platform's endiannness. There is also a clue in the title of the question. – Paul R Mar 02 '17 at 17:50
  • @Harry if you want to know how to transfer float variables over the network cross platform, and not just reverse the byte order: Edit your question erasing two words "byte order" from the title and from the body, then request to PaulR to reopen. – Rama Mar 02 '17 at 20:09
  • @Harry What you're looking for is something like htonf/ntohf like found in win32. On POSIX, you'd have to cast it. A method for this is provided [here](http://www.ridgesolutions.ie/index.php/2018/07/27/code-for-ntohf/). Can't answer because this question is marked as duplicate. – Unmanned Player Jun 21 '19 at 06:10
  • @PaulR Swapping endiannes isn't exactly what htons/htonl etc. do. It swaps on x86, but is likely going to be a blank macro in ARM/MIPS. Imho, op was searching for portable method to convert host order to network order and back, not just blindly swapping endiannes like the duplicate points to. – Unmanned Player Jun 21 '19 at 06:16

0 Answers0