strlen

PHP strlen and mb_strlen not working as expected

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: PHP functions strlen() and mb_strlen() both are returning the wrong number of characters when I run them on a string. Here is a piece of the code I'm using... $foo = mb_strlen($itemDetails['ITEMDESC'], 'UTF-8'); echo $foo; It also tells me that this string - "Infant Heel Warmer, No Adhesive Attachment Pad, 100/cs" is 54, which is correct. I assume its some issue with character encoding, everything should be UTF-8 I think. I've tried feeding mb_strlen() several different character encoding types and they all are returning this oddball count

Is There A Difference Between strlen()==0 and empty()?

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I was looking at some form validation code someone else had written and I saw this: strlen () == 0 When testing to see if a form variable is empty I use the empty() function. Is one way better than the other? Are they functionally equivalent? 回答1: strlen is to get the number of characters in a string while empty is used to test if a variable is empty Meaning of empty: empty ( "" ) //is empty for string empty ( 0 ) // is empty for numeric types empty ( null ) //is empty empty ( false ) //is empty for boolean 回答2: There are a couple

PHP shortest/longest string in array

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an array like this $data = array( "163", "630", "43", "924", "4", "54" ); How can I select the smallest and largest values from it according to string length NOT number value. (for this example it is 1 (smallest) and 3 (largest) . 回答1: Seems like you should use an array_map() // Convert array to an array of string lengths $lengths = array_map('strlen', $data); // Show min and max string length echo "The shortest is " . min($lengths) . ". The longest is " . max($lengths); Note that the $lengths array is unsorted, so you can easily

strlen()与sizeof()

大城市里の小女人 提交于 2019-12-03 02:29:55
一、strlen() strlen()为计算字符串长度的函数,以‘\0’为字符串结束标志。注意:其传入参数必须是字符串指针(char*), 当传入的是数组名时,实际上数组退化成指针了。 二、sizeof() sizeof()为运算符,用于计算所分配给元素的内存大小,其返回结果类型为size_t。 来源: https://www.cnblogs.com/socks/p/11714347.html

passing argument 1 of 'strlen' differ in signedness

匿名 (未验证) 提交于 2019-12-03 02:27:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I use strlen() call all over my project, until now I compiled my project without -Wall compiler option. But when I start using -Wall I face so many compiler warning. 80% are the strlen char * vs const char * warning. I am aware of type casting all strlen() calls. Is there any other way that I can suppress the following warning? ./Proj.c:3126: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness` C:/staging/usr/include/string.h:397: note: expected 'const char *' but argument is of type 'unsigned char *'` 回答1: strlen

Ambiguous call to overloaded function 'pow'

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having some problems runnning the following code. I got this: error C2668: 'pow' : ambiguous call to overloaded function. I've tried to manually cast the arguments to the appropiate type using static_cast, however I think I get some pointer errors?! The program should convert a number from base 16 to base 10. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <math.h> //base 16 to base 10 int convert(char *n){ int result = 0; for (int i = strlen(n) - 1; i >= 0; i--){ if

Objective-C sample code for HMAC-SHA1 [closed]

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to generate HMAC-SHA1 in Objective C. But i didnt find anything that works. I tried with CommonCrypto, using CCHMAC, but didnt works. I need to generate a hmac and after generate HOTP number. Somebody have any example code in Objective C or C? 回答1: Here's how you generate an HMAC using SHA-256: NSString *key; NSString *data; const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256,

Reversing a string in C

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I know this has been asked thousands of times but I just can't find the error in my code. Could someone kindly point out what I'm doing wrong? #include #include void reverseString ( char * myString ){ char temp ; int len = strlen ( myString ); char * left = myString ; // char *right = &myString[len-1]; char * right = myString + strlen ( myString ) - 1 ; while ( left 回答1: the problem is here char * somestring = "hello" ; somestring points to the string literal "hello". the C++ standard doesn't gurantee this, but on most machines,

C strings, strlen and Valgrind

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to understand why Valgrind is spitting out : ==3409== Invalid read of size 8 ==3409== at 0x4EA3B92: __GI_strlen (strlen.S:31) whenever I'm applying strlen on a dynamically allocated string? Here is a short testcase : #include #include #include int main() { char *hello = "Hello World"; char *hello2; /* Step 1 */ printf("Step 1\n"); printf("strlen : %lu\n",(unsigned long)strlen(hello)); /* Step 2 */ hello2 = calloc(12,sizeof(char)); hello2[0] = 'H'; hello2[1] = 'e'; hello2[2] = 'l'; hello2[3] = 'l'; hello2[4] = 'o'; hello2[5] = ' ';

strlen() and UTF-8 encoding

匿名 (未验证) 提交于 2019-12-03 00:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Assuming UTF-8 encoding, and strlen() in PHP, is it possible that this string has a length of 4? I'm only interested to know about strlen(), not other functions I have tested it on my own computer, and I have verified UTF-8 encoding, and the answer I get is 6. I don't see anything in the manual for strlen or anything I've read on UTF-8 that would explain why some of the characters above would count for less than one. PS: This question and answer (4) comes from a mock test for ZCE I bought on Ebay. PPS: Please throw me a bone and vote this up