How To Convert a 32-bit Integer into a 64 bit value In C

喜你入骨 提交于 2019-12-13 04:07:24

问题


I am getting compilation error in a Project Code where the situation is as follows:

typedef unsigned int U32bit;
typedef unsigned long long U64bit;

U32bit      var;
U64bit      var2;

var = function();  /* Function returns a 32-bit value, which is stored in var */

var2 = 100*var1;   /* 100*var1 is very Big & can be stored only in U64bit variable */

For the Above Line: var2 = 100*var1

I am getting the following Compilation Error on Solaris:

"conversion to non-scalar type requested"

I have also tried typecasting:

var2 = (U64bit) 100*var1;

This also gives the same error.


回答1:


What is U64bit? That is a non-standard type, so you must show its declaration.

It sounds as if it's a struct.




回答2:


The standard fixed-width integer types in C are uint32_t and uint64_t, try with these. Then a constant of that type can be defined with UINT64_C(100).

To have these types you might have to add

#include <stdint.h>

to your includes.




回答3:


Try the following:

var2 = (U64bit)var1 * 100;

EDIT

Perhaps

var2 = (U64bit)var1 * 100LL;

Anyway the declaration for U32Bit, U64Bit and forfunction would be useful.




回答4:


Are you sure that this type, "U64bit" is actually defined as an integer? If not, that could be the problem, that it could be a struct which is a scalar type.

You also don't specify which compiler or OS you are using. If it's anything following the C standard, you should #include <stdint.h> and use uint32_t and uint64_t instead of your U32bit and U64bit.



来源:https://stackoverflow.com/questions/13310241/how-to-convert-a-32-bit-integer-into-a-64-bit-value-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!