固定大小的数据类型。 [英] Fixed sized datatypes.

查看:79
本文介绍了固定大小的数据类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在空闲时间做一个项目。我遇到的一个问题是
就像short,int和long这样的东西在不同的平台上都有不同的含义。


在这个程序中,一些值只有16位的

长度非常重要。有没有办法做到这一点并使其便携?


提前致谢,

JB

解决方案

" Justin Barrett" <菊**** @ ezrs.com>在消息中写道

news:pa **************************** @ ezrs.com ... < blockquote class =post_quotes>我现在正在空闲时间做一个项目。我遇到的一个问题是,像short,int和long这样的东西在不同平台上的大小都有不同的含义。

在这个程序中它很漂亮重要的是,某些值只有16位长度。有没有办法做到这一点并使其可移植?




您可以创建一个特定于平台的头文件,其中包含

fixedde的typedef-大小类型,例如,

typedef short Int16;

typedef unsigned short Uint16;


仅使用Int16或Uint16类型您需要16位类型的代码。

当您移动到其他平台时,根据需要更改头文件

并重新编译。


DW


Justin Barrett写道:

我好我正在做我的项目空闲时间。我遇到的一个问题是,像short,int和long这样的东西在不同平台上的大小都有不同的含义。

在这个程序中它很漂亮重要的是,某些值只有16位长度。有没有办法做到这一点并使其可移植?




''unsigned short''保证有_at_least_ 16位。如果你需要

_exactly_ 16位,那你就不走运了。那里有平台

,根本没有任何小于32位的类型。你的代码可以

永远不会被移植到那里。如果你不在乎,你可以选择

使它便携。只有那些确实具有_a_类型的平台

才是16位。一旦你决定那就是你想要的,你只需要

创建一个带有条件编译部分的标题,每个部分都定义了

自己的justin_16bit_signed。类型:


#ifdef WIN32

typedef short justin_16bit_signed;

typedef unsigned shot justin_16bit_unsigned;

#else

....


Victor


Victor Bazarov写道:

#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...




这应该以编译器为条件,而不是操作系统。


#ifdef _MSC_VER

....


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Hi I''m currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it''s pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?

Thanks in advance,
JB

解决方案

"Justin Barrett" <ju****@ezrs.com> wrote in message
news:pa****************************@ezrs.com...

Hi I''m currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it''s pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?



You can create a platform-specific header file containing typedefs for
fixed-sized types, e.g.,
typedef short Int16;
typedef unsigned short Uint16;

Use only the Int16 or Uint16 types in your code where you need 16-bit types.
When you move to a different platform, alter the header file as necessary
and re-compile.

DW


Justin Barrett wrote:

Hi I''m currently doing a project in my free time. One problem I ran into
is that things like short, int, and long all have different meanings as to
size on different platforms.

In this program it''s pretty important that some values only be 16-bits in
length. Is there a way to do this and make it portable?



''unsigned short'' is guaranteed to have _at_least_ 16 bits. If you need
_exactly_ 16 bits, you are out of luck. There are platforms out there
that simply don''t have any types smaller than 32 bits. Your code could
never be ported there. If you don''t care about that, you can settle on
making it "portable" to only those platforms that do have _a_ type which
is exactly 16 bits. Once you decide that that''s what you want, you simply
create a header with conditionally compiled sections each of which defines
its own "justin_16bit_signed" type:

#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
....

Victor


Victor Bazarov wrote:

#ifdef WIN32
typedef short justin_16bit_signed;
typedef unsigned shot justin_16bit_unsigned;
#else
...



This should be conditionalized on the compiler, not the OS.

#ifdef _MSC_VER
....

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于固定大小的数据类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆