开发者

long to float conversion in linux kernel module

It's my first question here, and I hope that I'll get useful answers or hints here.

As it's in the title: I need to convert from long to float in a kernel module, for example: 78123456 to 78.123456, and the details as follows:

  1. I have two long variables (an out put of simple_strtol function) and I need to convert them to float (it's a required task in the project).
  2. The exponent is fixed as 6 (6 numbers after the floating point).
  3. (atof) doesn't work in kernel space.
  4. I tried to make casting...it doesn't work.
  5. I tried to times it by 0.000001, but float operations aren't available in kernel work.
  6. Of course, I'm working in C programming language.

Please, if there is any way to do that let me know asap:

  • if there is a function which I don't know (I don't thing there is one), what is it?
  • if there is a function one 开发者_开发百科library please tell me how can I download this library and how to include it.
  • if there is any other method also tell me.

Any hint is really appreciated.

Thank you very much.


This does not seem like it should be a kernel module at all. The tidbits described seem like they would be part of a fine application program which formats and sends UDP packets.

If that isn't viable, perhaps a FUSE-like approach could be used instead?


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) rather than actually manipulate the floating point variable (since you have already pointed out that floating point operations aren't available in the kernel space).

In this case, don't treat this problem as a conversion from long integer to floating point - instead treat it as a string manipulation problem, especially since your input to strtol is a string.

In pseudocode, if your input and output are separate strings:

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 the input as a long integer val, a line like

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

will do the right thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜