Convert to the Linux kernel for a long time

This is my first question here, and I hope I get helpful answers or tips here.

As in the title: I need to convert from long to float to the kernel module, for example: 78123456 - 78.123456, as well as the following data:

  • I have two long variables (exiting the simple_strtol function), and I need to convert them to float (this is the required task in the project).
  • The exponent is fixed as 6 (6 numbers after the floating point).
  • (atof) does not work in kernel space.
  • I tried to do a casting ... this does not work.
  • I tried to do this once at 0.000001, but floating-point operations are not available in the kernel.
  • Of course, I work in the C programming language.

Please, if there is a way to do this, let me know as soon as possible:

  • if there is a function that I don’t know (I don’t know what it is), what is it?
  • If there is a function in one library, please tell me how I can load this library and how to enable it.
  • if there is any other method, also tell me.

Any tips are really appreciated.

Thank you very much.

+4
source share
2 answers

It does not look like it should be a kernel module at all. The described tidbits remind you that they will be part of a great application program that formats and sends UDP packets.

If this is not practical, perhaps instead of FUSE-like ?

+1
source

My assumption is that you just want to display this variable with a decimal point (maybe the variable measures microseconds and you want to display in seconds), and not actually manipulate the variable with a floating point (since you already indicated that floating point operations not available in kernel space).

In this case, do not consider this problem as a conversion from a long integer to a floating point - instead, consider it as a string manipulation problem, especially since your input to strtol is a string.

In pseudo code, if your input and output are separate lines:

 void insertDecimalPoint(char const * strSrc, char * strDest, int maxlength) { 1. find the length of strSrc 2. if length <= 6, then write a prefix of zeros like '0.000' to strDest and then copy the source string to the destination 3. else, copy the first (length - 6) digits, then add a decimal point '.', then copy the rest of the source to the destination. } 

Alternatively, if you have input as a long integer val , a string like

 whole = val / 1e6; fraction = val - whole * 1e6; printf("%d.%06d", whole, fraction); 

will do the right thing.

0
source

All Articles