strtod

Are strtol, strtod unsafe?

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include #include int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or anything. (In fact, it wouldn't allow you to type bar as a const char * ,

return value of strtod() if string equals to zero

无人久伴 提交于 2019-12-02 01:28:47
As per MSDN: strtod returns 0 if no conversion can be performed or an underflow occurs. What if my string equals to zero (i.e., 0.0000)? How can I know if there is no error from the conversion? OK, I use the following code to verify the idea: char *Y = "XYZ"; double MyNum; char *MyEndPtr; int Err_Conversion = 0; errno = 0; //reset MyNum = strtod (Y, &MyEndPtr); if ( (MyNum == 0) && (errno != 0) && (strcmp(Y, MyEndPtr) == 0) ) { Err_Conversion = 1; } I see that MyNum = 0, but never see the content of Y copied into MyEnPtr, or errno = 0 in this forced error. Any idea? Use the str_end parameter

Python equivalent to C strtod

爷,独闯天下 提交于 2019-12-01 16:46:36
I am working on converting parts of a C++ program to Python, but I have some trouble replacing the C function strtod . The strings I'm working on consists of simple mathmatical-ish equations, such as "KM/1000.0". The problem is that the both constants and numbers are mixed and I'm therefore unable to use float(). How can a Python function be written to simulate strtod which returns both the converted number and the position of the next character? I'm not aware of any existing functions that would do that. However, it's pretty easy to write one using regular expressions: import re # returns

Python equivalent to C strtod

南笙酒味 提交于 2019-12-01 15:58:57
问题 I am working on converting parts of a C++ program to Python, but I have some trouble replacing the C function strtod. The strings I'm working on consists of simple mathmatical-ish equations, such as "KM/1000.0". The problem is that the both constants and numbers are mixed and I'm therefore unable to use float(). How can a Python function be written to simulate strtod which returns both the converted number and the position of the next character? 回答1: I'm not aware of any existing functions

Confused how to convert from a string to double using strtod() in C++

落花浮王杯 提交于 2019-12-01 10:38:26
If someone could explain how to use the function, that would be great. I don't understand the parameters. Thanks First parameter is a pointer to the chars. c_str() gives you that pointer from a string object. Second parameter is optional. It would contain a pointer to the next char after the numerical value in the string. See http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/ for more infos. string s; double d; d = strtod(s.c_str(), NULL); The first argument is a the string you want to convert, the second argument is a reference to a char* that you want to point to the first char

Confused how to convert from a string to double using strtod() in C++

半世苍凉 提交于 2019-12-01 05:37:07
问题 If someone could explain how to use the function, that would be great. I don't understand the parameters. Thanks 回答1: First parameter is a pointer to the chars. c_str() gives you that pointer from a string object. Second parameter is optional. It would contain a pointer to the next char after the numerical value in the string. See http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/ for more infos. string s; double d; d = strtod(s.c_str(), NULL); 回答2: The first argument is a the string

What is the result of `strtod(“3ex”, &end)` supposed to be? What about `sscanf`?

元气小坏坏 提交于 2019-11-30 09:03:29
In my experiments this expression double d = strtod("3ex", &end); initializes d with 3.0 and places end pointer at 'e' character in the input string. This is exactly as I would expect it to behave. The 'e' character might look as a beginning of the exponent part, but since the actual exponent value (required by 6.4.4.2) is missing, that 'e' should be treated as a completely independent character. However, when I do double d; char c; sscanf("3ex", "%lf%c", &d, &c); I notice that sscanf consumes both '3' and 'e' for the %lf format specifier. Variable d receives 3.0 value. Variable c ends up with

Problem with string conversion to number ( strtod )

不羁岁月 提交于 2019-11-29 14:02:53
I am using strtod( ) function to extract an environment variable as a string, and then changing it to double using strtod: enter code here char strEnv[32]; strncpy(strEnv, getenv("LT_LEAK_START"), 31); // How to make sure before parsing that env LT_LEAK_START is indeed a number? double d = strtod(strEnv, NULL); Now i want to make sure that this number entered by user is a number and not a string or special character. How can i make sure of that? A code snippet would be of great help. Thanks in advance. The 2nd argument to the strtod function is useful. char *err; d = strtod(userinput, &err);

Converting char* to float or double

ε祈祈猫儿з 提交于 2019-11-26 17:48:17
问题 I have a value I read in from a file and is stored as a char*. The value is a monetary number, #.##, ##.##, or ###.##. I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage numbers. What is the correct way to do this, and why is the way I am doing it wrong? This is essentially what I am doing, just the char* value is read in from a file. When I print out the temp and ftemp variables they are just garbage, gigantic negative

How to convert string to float?

▼魔方 西西 提交于 2019-11-26 07:28:58
问题 #include<stdio.h> #include<string.h> int main() { char s[100] =\"4.0800\" ; printf(\"float value : %4.8f\\n\" ,(float) atoll(s)); return 0; } I expect the output should be 4.08000000 whereas I got only 4.00000000 . Is there any way to get the numbers after the dot? 回答1: Use atof() or strtof() * instead: printf("float value : %4.8f\n" ,atof(s)); printf("float value : %4.8f\n" ,strtof(s, NULL)); http://www.cplusplus.com/reference/clibrary/cstdlib/atof/ http://www.cplusplus.com/reference/cstdlib