正确的填充 [英] right padding

查看:47
本文介绍了正确的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


是否可以右键填充0。 (或其他字符!=空白)?

例如:

1,长度10 =" 1000000000"
我试过了与sprintf,但我只能留下0的垫。或右垫

with blanks =>

" 0000000001"

" 1"


谢谢。

Hello,

Is it possible to right pad with "0" (or other character != blank) ?
for example :
1 , length 10 ="1000000000"
I''ve tried with sprintf but I can only left pad with "0" or right pad
with blanks =>
"0000000001"
"1 "

Thanks.

推荐答案

marc说:
marc said:

你好,


是否可以右键填充0。 (或其他字符!=空白)?

例如:

1,长度10 =" 1000000000"
我试过了与sprintf,但我只能留下0的垫。或右垫

with blanks =>

" 0000000001"

" 1"
Hello,

Is it possible to right pad with "0" (or other character != blank) ?
for example :
1 , length 10 ="1000000000"
I''ve tried with sprintf but I can only left pad with "0" or right pad
with blanks =>
"0000000001"
"1 "



sprintf不允许你选择填充字符。但是编写一个函数为你做填充很容易




#include< stdio.h>

#include< assert.h>

#include< limits.h>


char * pad(char * s,int n ,size_t width,int padch)

{

int count = 0;

char * t = s;

断言(宽度(sizeof n * CHAR_BIT + 5)/ 3);

count = sprintf(t,"%d",n);

t + = count ;

while(count< len)

{

* t ++ = padch;

}

* t =''\ 0'';

返回s;

}


保持与comp.lang.c的精神,上面是未经测试的(因为

我赶时间)。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

sprintf doesn''t let you choose the padding character. But it''s pretty easy
to write a function to do the padding for you:

#include <stdio.h>
#include <assert.h>
#include <limits.h>

char *pad(char *s, int n, size_t width, int padch)
{
int count = 0;
char *t = s;
assert(width (sizeof n * CHAR_BIT + 5) / 3);
count = sprintf(t, "%d", n);
t += count;
while(count < len)
{
*t++ = padch;
}
*t = ''\0'';
return s;
}

In keeping with the spirit of comp.lang.c, the above is untested (because
I''m in a hurry).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


10月21日凌晨4点17分*,Richard Heathfield< rj *@see.sig.invalidwrote:
On Oct 21, 4:17*am, Richard Heathfield <rj*@see.sig.invalidwrote:

marc说:
marc said:

你好,
Hello,


是否可以右键填充0。 (或其他字符!=空白)?

例如:

1,长度10 =" 1000000000"
我试过了与sprintf,但我只能留下0的垫。或右垫

with blanks =>

" 0000000001"

" 1 * * * *"
Is it possible to right pad with "0" (or other character != blank) ?
for example :
1 , length 10 ="1000000000"
I''ve tried with sprintf but I can only left pad with "0" or right pad
with blanks =>
"0000000001"
"1 * * * * "



sprintf不允许你选择填充字符。但是编写一个函数为你做填充很容易




#include< stdio.h>

#include< assert.h>

#include< limits.h>


char * pad(char * s,int n ,size_t width,int padch)

{

* int count = 0;

* char * t = s;

*断言(宽度(sizeof n * CHAR_BIT + 5)/ 3);


sprintf doesn''t let you choose the padding character. But it''s pretty easy
to write a function to do the padding for you:

#include <stdio.h>
#include <assert.h>
#include <limits.h>

char *pad(char *s, int n, size_t width, int padch)
{
* int count = 0;
* char *t = s;
* assert(width (sizeof n * CHAR_BIT + 5) / 3);



这是什么意思?让我们说int是四个字节长。 ''>''右侧的

表达式将产生37/3(12)。什么

''宽度'代表什么? (你没有在

函数中的任何其他地方使用它。)

What''s the purpose of this? Let''s say that int is four bytes long. The
expression on the right side of the ''>'' will yield 37 / 3 (12). What
does ''width'' represent? (You aren''t using it anywhere else in the
function.)


* count = sprintf(t,"%d" ;,n);

* t + = count;

* while(count< len)
* count = sprintf(t, "%d", n);
* t += count;
* while(count < len)



''len''未定义。

''len'' is undefined.


* {

* * * t ++ = padch;

*}
* {
* * *t++ = padch;
* }



这将产生无限循环。

And that will yield an infinite loop.


* * t =''\ 0'';

*返回s;


}


符合comp.lang的精神.c,以上是未经测试的(因为

我很匆忙)。
* *t = ''\0'';
* return s;

}

In keeping with the spirit of comp.lang.c, the above is untested (because
I''m in a hurry).



这是一个更安全和测试的版本:


char * PadSnprintf(char * str,//缓冲区

int n,// sizeof(str)

int item,//要转换的项目

char paddingChar)//填充字符< br $>
{

snprintf(str,n,"%d",item);


char * p = str + strlen (str);

while(p< str + n - 1)

* p ++ = paddingChar;


* p =''\ 0'';

返回str;

}


塞巴斯蒂安

Here''s a safer and tested version:

char *PadSnprintf(char *str, // the buffer
int n, // sizeof(str)
int item, // the item to convert
char paddingChar) // the padding character
{
snprintf(str, n, "%d", item);

char *p = str + strlen(str);
while (p < str + n - 1)
*p++ = paddingChar;

*p = ''\0'';
return str;
}

Sebastian


s0****@gmail.com 说:

10月21日凌晨4:17,Richard Heathfield< rj *@see.sig.invalidwrote:
On Oct 21, 4:17 am, Richard Heathfield <rj*@see.sig.invalidwrote:



<剪断>

<snip>


>断言(width(sizeof n * CHAR_BIT + 5)/ 3);
>assert(width (sizeof n * CHAR_BIT + 5) / 3);



这是什么意思?让我们说int是四个字节长。 ''>''右侧的

表达式将产生37/3(12)。


What''s the purpose of this? Let''s say that int is four bytes long. The
expression on the right side of the ''>'' will yield 37 / 3 (12).



不,对于四字节的整数,它产生*至少* 12.如果字节宽于

八位,这是合法的,它会给出更大的价值。

No, for four-byte ints it yields *at least* 12. If bytes are wider than
eight bits, which is legal, it will give a larger value.


''width''代表什么?
What does ''width'' represent?



它表示从s开始的缓冲区宽度。它必须足够大

来保存整数的文本表示。如果int是

大小的四个八位字节,那么它可以保存一个值,其基本十个文本表示为

large为-2147483648,这是11个字节长,所以缓冲区必须在
至少十二个字节长(一个用于空终止符)。

It represents the width of the buffer starting at s. It must be big enough
to hold the text representation of an integer. If int is four octets in
size, then it could hold a value whose base ten text representation is as
large as -2147483648, which is eleven bytes long, so the buffer must be at
least twelve bytes long (one for the null terminator).


(你不是在其他任何地方使用它在函数中。)
(You aren''t using it anywhere else in the function.)



我的意思是使用它...

I did mean to use it...


> count = sprintf(t,"%d",n);
t + = count;
while(count< len)
>count = sprintf(t, "%d", n);
t += count;
while(count < len)



''len''未定义。


''len'' is undefined.



....这里。我的意思是宽度,而不是len。

....here. I meant width, not len.


>
>

> {
* t ++ = padch;
}
>{
*t++ = padch;
}



And这将产生无限循环。


And that will yield an infinite loop.



嗯,现在你只是挑剔了。 :-)

Well, now you''re just being picky. :-)


这是一个更安全和测试的版本:


char * PadSnprintf(char * str,//缓冲区

int n,// sizeof(str)
Here''s a safer and tested version:

char *PadSnprintf(char *str, // the buffer
int n, // sizeof(str)



如果评论正确,为什么需要n?为什么不呢?使用sizeof(str)?

但我怀疑评论是错误的。

If the comment is correct, why do you need n? Why not just use sizeof(str)?
But I suspect the comment is wrong.


int item,//要转换的项目

char paddingChar)//填充字符

{

snprintf(str,n,"%d",item);
int item, // the item to convert
char paddingChar) // the padding character
{
snprintf(str, n, "%d", item);



马上,你的更安全和经过测试的版本无法链接到无法访问snprintf的大量数据。


您在我的代码中发现的两个错误(而且我感谢你们这是非常好的修理。在系统上实施snprintf并不是那么简单。缺少它的



-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Right away, your "safer and tested version" fails to link on a vast number
of C installations that don''t have access to snprintf.

The two bugs that you spotted in my code (and I thank you for that) are
trivial to fix. It''s rather less trivial to implement snprintf on systems
that lack it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于正确的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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