Tag: string

在C中返回错误的MD5哈希

我正在尝试使用原始/未触摸的md5.h和来自http://www.arp.harvard.edu的 md5c.c为字符串“Hello World”生成MD5哈希。 但我的结果与我测试的所有md5在线工具不同。 这个代码怎么了? 谢谢。 #include #include #include #include “md5.h” void MD5hash(unsigned char *data, unsigned int dataLen, unsigned char *digest) { MD5_CTX c; MD5Init(&c); MD5Update(&c, data, dataLen); MD5Final(digest, &c); } int main(int argc, const char * argv[]) { unsigned char digest[16]; const char *s = “Hello World”; unsigned int l = (unsigned int)strlen(s); MD5hash((unsigned […]

C ++中的C字符串删除

什么是好的,什么是错的? 在这种情况下,我是否必须调用delete以防止内存泄漏? 此外,C和C ++中的行为是否完全相同? 有什么不同吗? const char* a = “blahblah”; … delete a; char b* = new char(‘a’); … delete b; char c[100] = “blahblah”; … delete c; char d* = new char[40]; … delete d; char e* = new char[40]; … delete[] e;

pascal字符串如何在内存中表示?

pascal字符串如何在内存中排列? 我读到: http : //www.freepascal.org/docs-html/ref/refsu12.html它说字符串存储在堆上并且引用计数。 为了确定存储长度和引用的位置,我创建了一个字符串并对其进行了大量测试: type PInt = ^Integer; var str: String; begin str := ‘hello’; writeln(PInt(@str[1]) – (sizeof(integer) * 1)); //length writeln(PInt(@str[1]) – (sizeof(integer) * 2)); //reference count end. 第一个打印长度,第二个打印参考计数。 它做得非常好并且有效。 现在我试图在C中模仿同样的事情: Export char* NewCString() { const char* hello_ptr = “hello”; int length = strlen(hello_ptr); //allocate space on the heap for: sizeof(refcount) + […]

使用C编程语言将String转换为Boolean

我有一个简单的问题。 如何在C中将字符串变量转换为boleean? 样品: char array[] = “(1==1)”; 如何将array转换为布尔值,以便我可以将其放入?

C字符串在C和C ++中为大写

当我在C ++中组合一个大写的函数时,我注意到我没有在C中收到预期的输出。 C ++函数 #include #include #include void strupp(char* beg) { while (*beg++ = std::toupper(*beg)); } int main(int charc, char* argv[]) { char a[] = “foobar”; strupp(a); printf(“%s\n”, a); return 0; } 按预期输出: FOOBAR Cfunction #include #include #include void strupp(char* beg) { while (*beg++ = toupper(*beg)); } int main(int charc, char* argv[]) { char a[] […]

将非空终止字符串传递给printf会导致意外值

这个C程序给出了一个奇怪的结果: #include #include int main(int argc, char *argv[]) { char str1[5] = “abcde”; char str2[5] = ” haha”; printf(“%s\n”, str1); return 0; } 当我运行这段代码时,我得到: abcde haha 我只想打印第一个字符串,从代码中可以看出。 为什么要打印它们?

当strlen产生分段错误时,来自GetString()的C字符串

我在C中运行程序。当我运行程序时,我得到了分段错误错误。 在我回溯时它在gdb中告诉我 程序接收信号SIGSEGV,分段故障。 __strlen_sse2_bsf()at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:51 51 movdqu(%edi),%xmm1 我相信它与strlen有关。 我使用strlen的唯一一次是: string s = GetString(); int stringlength = strlen(s); 当我更改strlen for sizeof错误停止时。 我的代码出了什么问题? GetString的文档 /* * Reads a line of text from standard input and returns it as a * string (char *), sans trailing newline character. (Ergo, if * user inputs only “\n”, returns “” not […]

C多维数组字符串

我正在声明一个字符串数组非常简单,硬编码,但它一直给我array type has incomplete element type错误。 我想这与每个数组的长度有关但我不知道如何修复它而不设置字符串的固定长度。 char allocate[][2][] = { // Error with or without the 2 {“value1″,”value2”}, {“value3″,”value4”} };

为什么我不能得到整个字符串的长度?

我试过strlen(字符串)。 我尝试编写代码来查找整个字符串的长度。 然而,当达到第一个“空间”时,它仍然倾向于停止计算长度。 为什么会这样? 解决办法是什么? 我试图停在’\ 0’。 这不应该工作吗? 这是代码的一部分。 int main(void) { char phrase[256]; char phrase2[256]; int i,j,size; scanf(“%255s”, phrase); for(i=0;phrase[i]!=’\0′;i++){ size++; } …

从字符串C获取int值

鉴于我有以下内容: char str[] = “1524”; int nr; 我想在’nr’中得到1524号码。 在C中实现这一目标的最佳方法是什么?