奇怪的输出问题 [英] Strange output problem

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

问题描述

大家好,


我在尝试使用Kevin Easton的base64

加密算法时遇到了一些问题。 (参见
http://groups.google.com/groups?selm...to.pcug.org.au


这是我用来测试的示例程序algorythm。


-----启动main.c -----


#include< stdio.h>

#include" base64.h"


int main(无效)

{

char string [5] =" hello";

int len;

char string2 [10];

len = strlen(string);

printf("%s \ n",string);

b64_encode(string2,string,len);

printf(" ;%s \ n",string2);


返回0;

}

-----结束主.c -----


我使用以下方法编译程序:


gcc base64.c main.c -o base


当我运行它时,我得到以下输出。

mick @ codegurus $ ./base

hello ??(? ????

aGVsbG / wBCjG7L + / AQ == ???????(


有谁知道哪里出错了?

Hi Everyone,

I am having a few issues while attempting to use Kevin Easton''s base64
encryption algorythm. (See
http://groups.google.com/groups?selm...to.pcug.org.au)

here is a sample program I am using to test the algorythm.

----- start main.c -----

#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
int len;
char string2[10];
len = strlen(string);
printf("%s\n", string);
b64_encode(string2, string, len);
printf("%s\n", string2);

return 0;
}
----- end main.c -----

I compile the program using:

gcc base64.c main.c -o base

And when I run it I get the following output.
mick@codegurus $ ./base
hello?(????
aGVsbG/wBCjG7L+/AQ==??????(

Does anyone know where its going wrong?

推荐答案

./ base

hello ??(?????

aGVsbG / wBCjG7L + / AQ == ???????(


有谁知道它出了什么问题?
./base
hello?(????
aGVsbG/wBCjG7L+/AQ==??????(

Does anyone know where its going wrong?





Materialized写道:


Materialised wrote:
大家好,

我在尝试使用Kevin Easton的base64时遇到了一些问题
加密algorythm。 (参见
< snip> char string [5] =" hello";
Hi Everyone,

I am having a few issues while attempting to use Kevin Easton''s base64
encryption algorythm. (See <snip> char string[5] = "hello";




需要大小为6才能保留空字符。


Ed。



Needs to be size "6" to hold the null character.

Ed.


我会好好的!NEWBIE ANSWER - 可能充满了错误也是:)


物化写道:
I''ll have a go! NEWBIE ANSWER -- probably full of errors too :)

Materialised wrote:
#include< stdio.h>
#include" base64.h"

int main(void)
{char string [5] =" hello" ;;


string [0] =''h''; / * this * /

string [1] =''e''; / *使* /

string [2] =''l''; / * string * /

string [3] =''l''; / * 5个字符* /

string [4] =''o''; /* 长! * /

/ *" hello"需要至少6个字符:'''','''','''','''','''',''\ 0''* /


不再分配字符


- > UB(未定义的行为)???


您试图将''\ 0''复制到您不拥有的内存中。

您可能希望让编译器决定数组大小:

char string [] =" hello";


或为常量指定足够的大小

char string [32] =" hello";

int len;
char string2 [10];
len = strlen(string) ;


我认为strlen()会在最后''o''检查null

终止符后继续,这可以是_anywhere_

printf("%s \ n",string);


这里同样的东西

b64_encode(string2,string,len);


和这里

printf("%s \ n",string2);

返回0;
}

-----结束main.c -----

我使用以下方法编译程序:

gcc base64.c main.c -o base

当我运行它时,我得到以下输出。
mick @ codegurus
#include <stdio.h>
#include "base64.h"

int main(void)
{
char string[5] = "hello";
string[0] = ''h''; /* this */
string[1] = ''e''; /* makes */
string[2] = ''l''; /* string */
string[3] = ''l''; /* 5 chars */
string[4] = ''o''; /* long! */
/* "hello" needs at least 6 chars: ''h'', ''e'', ''l'', ''l'', ''o'', ''\0'' */

no more chars have been allocated

--> UB (undefined behaviour) ???

you are trying to copy ''\0'' to a place in memory you do not own.
You may want to let the compiler decide on the array size:
char string[] = "hello";

or specify enough size for your constants
char string[32] = "hello";
int len;
char string2[10];
len = strlen(string);
I think strlen() will go on after the final ''o'' checking for the null
terminator which can be _anywhere_
printf("%s\n", string);
same thing here
b64_encode(string2, string, len);
and here
printf("%s\n", string2);

return 0;
}
----- end main.c -----

I compile the program using:

gcc base64.c main.c -o base

And when I run it I get the following output.
mick@codegurus


这篇关于奇怪的输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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