整数到“字符串”转换 [英] Integer to "string" conversions

查看:65
本文介绍了整数到“字符串”转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我阅读了faq,它建议使用sprintf。但是,

我想知道字符串中整数的完成位置。

基本上,我想:


nbr |其他数据


但其他数据的所有方式都必须从同一个地方开始。我有一些问题使用sprintf来完成这个要求。

也许

我忽略了一些东西。但是sprintf将nbr完全翻译成

字符串,所以nbr 123最终会占用:


p [0 ] =''1''

p [1] =''2''

p [2] =''3''


随着数量的增加,占用的空间越来越大。


为了解决这个问题,我选择了下面的解决方案。但是这需要



以后要退回数字。有没有更好的方法呢?


注意:我使用uint32_t来表示32位无符号整数。这个

是具体的实施

,但为了这个讨论,我需要知道

的大小
$ b $提前分配给buf的整数的b。我也是

忽略

动态分配数组也用于讨论。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int

main(void){


int i;

uint32_t nbr;

unsigned char buf [4];


nbr = 0xffffffff;

memset(buf,0,sizeof buf);


buf [0] =( unsigned char)(nbr> 24)& 0xff;

buf [1] =(unsigned char)(nbr> 16)& 0xff;

buf [2] =(unsigned char)(nbr> 8)& 0xff;

buf [3] =(unsigned char)nbr& 0xff;


for(i = 0; i< 4; i ++)

printf("%d \ nn",buf [i] );


退出(EXIT_SUCCESS);

}

解决方案

bw*****@yahoo.com 写道:


现在,我读了faq,它建议使用sprintf。但是,

我想知道字符串中整数的完成位置。

基本上,我想:


nbr |其他数据


但其他数据的所有方式都必须从同一个地方开始。我有一些问题使用sprintf来完成这个要求。

也许

我忽略了一些东西。 [...]



可能类似于%5d的事情或者%05d。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid


bw ***** @ yahoo.com 写道:


现在,我读了faq,它建议使用sprintf。但是,

我想知道字符串中整数的完成位置。

基本上,我想:


nbr |其他数据


但其他数据的所有方式都必须从同一个地方开始。我有一些问题使用sprintf来完成这个要求。

也许

我忽略了一些东西。但是sprintf将nbr完全翻译成

字符串,所以nbr 123最终会占用:


p [0 ] =''1''

p [1] =''2''

p [2] =''3''


随着数量的增加,占用的空间越来越大。


为了解决这个问题,我选择了下面的解决方案。但是这需要



以后要退回数字。有没有更好的方法呢?


注意:我使用uint32_t来表示32位无符号整数。这个

是具体的实施

,但为了这个讨论,我需要知道

的大小
$ b $提前分配给buf的整数的b。我也是

忽略

动态分配数组也用于讨论。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int

main(void){


int i;

uint32_t nbr;

unsigned char buf [4];


nbr = 0xffffffff;

memset(buf,0,sizeof buf);


buf [0] =( unsigned char)(nbr> 24)& 0xff;

buf [1] =(unsigned char)(nbr> 16)& 0xff;

buf [2] =(unsigned char)(nbr> 8)& 0xff;

buf [3] =(unsigned char)nbr& 0xff;


for(i = 0; i< 4; i ++)

printf("%d \ nn",buf [i] );


退出(EXIT_SUCCESS);

}



你的生活远远不够太难了。


A)如果你使用了以下内容:


char buf [25];

unsigned int num = 3784;

sprintf(buf,"%d",num);


你可以轻松使用strlen(buf)来计算sprintf()

结果有多长。


B)为什么不将所有字符串格式组合成一个进程

使用类似的东西:


char buf [100];

unsigned int num = 23405;

unsigned int num2 = 978 ;

char name [] =" Ben Hurr" ;;

sprintf(buf,"%10d%s - %d \ n",num,name, num2);


- mkaras


bw ***** @ yahoo.com 写道:

>

现在,我读了faq,它建议使用sprintf。但是,

我想知道字符串中整数的完成位置。

基本上,我想:


nbr |其他数据


但其他数据的所有方式都必须从同一个地方开始。我有一些问题使用sprintf来完成这个要求。

也许

我忽略了一些东西。但是sprintf将nbr完全翻译成

字符串,所以nbr 123最终会占用:


p [0 ] =''1''

p [1] =''2''

p [2] =''3''



sizeof" 123"是四个,而不是三个。


p [3] =''\ 0''


-

pete


Now, I read the faq, and it suggests using sprintf. However,
I want to all ways know where the integer finishes in the string.
Basically, I want to:

nbr | other data

But the other data all ways has to start at the same place. I had
some problems using sprintf to accomplish this requirement. Maybe
I am overlooking something. But sprintf translates the nbr exactly
into
the string, so the nbr 123, would end up occupying:

p[0] = ''1''
p[1] = ''2''
p[2] = ''3''

So as the number grew, the space taken grew.

To solve this, I went with the solution below. But this would require
me
to OR back the number later. Is there a better way to do this?

Note: I am using uint32_t to signify a 32 bit unsigned integer. This
is implementation
specific, but for the sake of this discussion, I need to know
the size
of the integer being assigned to buf ahead of time. I am also
ignoring
dynamically allocated arrays for this discussion as well.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(void) {

int i;
uint32_t nbr;
unsigned char buf[4];

nbr = 0xffffffff;
memset(buf, 0, sizeof buf);

buf[0] = (unsigned char)(nbr >24) & 0xff;
buf[1] = (unsigned char)(nbr >16) & 0xff;
buf[2] = (unsigned char)(nbr > 8) & 0xff;
buf[3] = (unsigned char) nbr & 0xff;

for(i = 0; i < 4; i++)
printf("%d\n", buf[i]);

exit(EXIT_SUCCESS);
}

解决方案

bw*****@yahoo.com wrote:

Now, I read the faq, and it suggests using sprintf. However,
I want to all ways know where the integer finishes in the string.
Basically, I want to:

nbr | other data

But the other data all ways has to start at the same place. I had
some problems using sprintf to accomplish this requirement. Maybe
I am overlooking something. [...]

Probably things like "%5d" or "%05d".

--
Eric Sosman
es*****@acm-dot-org.invalid


bw*****@yahoo.com wrote:

Now, I read the faq, and it suggests using sprintf. However,
I want to all ways know where the integer finishes in the string.
Basically, I want to:

nbr | other data

But the other data all ways has to start at the same place. I had
some problems using sprintf to accomplish this requirement. Maybe
I am overlooking something. But sprintf translates the nbr exactly
into
the string, so the nbr 123, would end up occupying:

p[0] = ''1''
p[1] = ''2''
p[2] = ''3''

So as the number grew, the space taken grew.

To solve this, I went with the solution below. But this would require
me
to OR back the number later. Is there a better way to do this?

Note: I am using uint32_t to signify a 32 bit unsigned integer. This
is implementation
specific, but for the sake of this discussion, I need to know
the size
of the integer being assigned to buf ahead of time. I am also
ignoring
dynamically allocated arrays for this discussion as well.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(void) {

int i;
uint32_t nbr;
unsigned char buf[4];

nbr = 0xffffffff;
memset(buf, 0, sizeof buf);

buf[0] = (unsigned char)(nbr >24) & 0xff;
buf[1] = (unsigned char)(nbr >16) & 0xff;
buf[2] = (unsigned char)(nbr > 8) & 0xff;
buf[3] = (unsigned char) nbr & 0xff;

for(i = 0; i < 4; i++)
printf("%d\n", buf[i]);

exit(EXIT_SUCCESS);
}

You are making your life far far too hard.

A) If you you used the following:

char buf[25];
unsigned int num = 3784;
sprintf(buf, "%d", num);

you could easily use strlen(buf) to figure out how long the sprintf()
result is.

B) Why not combine all the string formatting together into one process
using something like:

char buf[100];
unsigned int num = 23405;
unsigned int num2 = 978;
char name[] = "Ben Hurr";
sprintf(buf, "%10d %s - %d\n", num, name, num2);

- mkaras


bw*****@yahoo.com wrote:

>
Now, I read the faq, and it suggests using sprintf. However,
I want to all ways know where the integer finishes in the string.
Basically, I want to:

nbr | other data

But the other data all ways has to start at the same place. I had
some problems using sprintf to accomplish this requirement. Maybe
I am overlooking something. But sprintf translates the nbr exactly
into
the string, so the nbr 123, would end up occupying:

p[0] = ''1''
p[1] = ''2''
p[2] = ''3''

sizeof "123" is four, not three.

p[3] = ''\0''

--
pete


这篇关于整数到“字符串”转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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