将未知长度的int转换为字符串 [英] Converting int of unknown length to a string

查看:80
本文介绍了将未知长度的int转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写需要在各种Unix系统上运行的代码。我是调用statvfs和statfs系统调用的
,我需要转换一些

返回字符串的整数。通常我会使用

sprintf这样做:

sprintf(& buffer,"%lu",整数);


问题是我不知道我转换的整数值是多长还是长。我也不知道使用的编译器是否支持长期整理。那么有没有办法将未知整数转换为

一个字符串而不指定什么类型的整数?


itoa()会长时间工作吗?如果它没有,是否有任何类似的调用

将对任何传递给它的int工作?


重申我需要做的事情,这里有一些伪代码:


struct statfs fsstruct;

statfs(" /" ;,& amp; fsstruct);

//获取fsstruct.f_blocks值,这是一个未知长度的int并将其改为

为一个字符串

// fsstruct.f_blocks可能是一个短整数,长int,long long int等

//我也不知道是否支持多头支票


我是否比这更难?


谢谢

I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Will itoa() work on a long long? If it doesn''t, are there any calls like it
that will work on any int passed to it?

To reiterate what I need to do, here is some pseudo code:

struct statfs fsstruct;
statfs("/", &fsstruct);
// Take fsstruct.f_blocks value which is an int of unknown length and change
it to a string
// fsstruct.f_blocks could be a short int, long int, long long int, etc
// I also have no idea if long longs are supported

Am I making this harder than it is?

Thanks

推荐答案

TomServo写道:
TomServo wrote:
我正在编写需要的代码在各种Unix系统上运行。我正在调用statvfs和statfs系统调用,我需要转换一些返回字符串的整数。通常情况下我会使用sprintf这样做:
sprintf(& buffer,"%lu",整数);

问题是我不知道是否我转换的整数值长或长。我也不知道使用的编译器是否支持长时间的整数。那么有没有办法将一个未知的整数转换为一个字符串而不指定什么样的整数?

itoa()会工作很长时间吗?如果它没有,是否有任何类似的调用
会对传递给它的任何int有效吗?
I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Will itoa() work on a long long? If it doesn''t, are there any calls like it
that will work on any int passed to it?



尝试(u)lltostr,假设你知道你的是否价值是否签署。


-

Ian Collins。


Try (u)lltostr, assuming you know whether your values are signed or not.

--
Ian Collins.


Ian Collins写道:
Ian Collins wrote:
TomServo写道:
TomServo wrote:
我正在编写需要在各种Unix系统上运行的代码。我正在调用statvfs和statfs系统调用,我需要转换一些返回字符串的整数。通常情况下我会使用sprintf这样做:
sprintf(& buffer,"%lu",整数);

问题是我不知道是否我转换的整数值长或长。我也不知道使用的编译器是否支持长时间的整数。那么有没有办法将一个未知的整数转换为一个字符串而不指定什么样的整数?

itoa()会工作很长时间吗?如果它没有,是否有任何类似的调用
会对传递给它的任何int有效吗?
I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Will itoa() work on a long long? If it doesn''t, are there any calls like it
that will work on any int passed to it?


尝试(u)lltostr,假设您知道您的值是否已签名或没有。


Try (u)lltostr, assuming you know whether your values are signed or not.




1. lttostr不是标准的C函数,它会让它偏离主题

这里。

2.我从未在Solaris以外的任何系统上看过这个函数所以

即使在Unix系统中,我也不会把它称为非常便携。

3。如果编译器不支持long long int类型,则返回long long值的函数

可能无法使用这样的
编译器调用。


Robert Gamble



1. lltostr is not a Standard C function which would make it off-topic
here.
2. I have never seen this function on any system outside of Solaris so
I wouldn''t call it very portable, even across Unix systems.
3. If the compiler does not support type long long int then a function
that returns a long long value probably won''t be callable with such a
compiler.

Robert Gamble


我认为你可以定义一个结构;

struct myInteger {

BYTE类型; //表达数据类型。 int或long;

union {

int i;

long i;

}数据;

}

I think you can define a struct;
struct myInteger {
BYTE type; // express data type. int or long;
union {
int i;
long i;
} Data;
}


这篇关于将未知长度的int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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