Questions tagged [string-conversion]

String conversion is the act of converting data to and from character strings. It is a special case of type conversion and commonly used in programming to combine multiple data types into a string or when validating/storing user input.

377 questions
22
votes
6 answers

C++ convert simple values to string

Right now I use the following piece of code to dummily convert basic types (int, long, char[], this kind of stuff) to std::string for further processing: template constexpr std::string stringify(const T& t) { std::stringstream ss; …
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
19
votes
5 answers

Convert string to number & vice versa complexity

What would be the complexity of converting a string to its equivalent number or vice versa? Does it change depending on programming language? On the face of it, one needs to traverse the entire string to convert it to a number, so it is O(n), or…
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
15
votes
2 answers

Is C++17 std::to_chars adding a null terminator?

http://en.cppreference.com/w/cpp/utility/to_chars Reference does not say anything about that, but the example is (for me) clearly using a null-terminated string, otherwise how could it know where to end, since std::array::data returns only a…
Poeta Kodu
  • 1,120
  • 8
  • 16
11
votes
3 answers

How to convert st_mtime (which get from stat function) to string or char

I need to convert st_mtime to string format for passing it to java layer, i try to use this example http://www.cplusplus.com/forum/unices/10342/ but compiler produce errors invalid conversion from 'long unsigned int*' to 'const time_t* {aka …
testCoder
  • 7,155
  • 13
  • 56
  • 75
10
votes
3 answers

String to binary output in Java

I want to get binary (011001..) from a String but instead i get [B@addbf1 , there must be an easy transformation to do this but I don't see it. public static String toBin(String info){ byte[] infoBin = null; try { infoBin = info.getBytes(…
Nick
  • 605
  • 3
  • 11
  • 27
10
votes
2 answers

How can I get the traceback object ( sys.exc_info()[2] , same as sys.exc_traceback ) as a string?

I have a function which catches all exceptions, and I want to be able to get the traceback as a string within this function. So far this is not working: def handle_errors(error_type, error_message, error_traceback): """catch errors""" …
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
10
votes
2 answers

Conversion from string to char - c++

For a program I'm writing based on specifications, a variable is passed in to a function as a string. I need to set that string to a char variable in order to set another variable. How would I go about doing this? This is it in the header file: void…
ModdedLife
  • 669
  • 2
  • 11
  • 24
9
votes
8 answers

Can you cast a LPTSTR to a BSTR?

Is it legal to cast a LPTSTR directly to a BSTR? Based on my understanding of BSTR, casting a LPTSTR to a BSTR directly will leave you with a corrupted length prefix. The example code explicitly states that a string literal cannot be stored to a…
Mashmagar
  • 2,556
  • 2
  • 29
  • 38
8
votes
2 answers

How to convert nsstring to cString?

I have a nsstring(filePath), which has the path to the audio file. I want open the audio file, so I want to convert the nsstring to Cstring. fopen([filePath cStringUsingEncoding:1], "r"); is the above line is correct or not, because I can also…
Warrior
  • 39,156
  • 44
  • 139
  • 214
8
votes
7 answers

Check if the input is a number or string in C++

I wrote the following code to check whether the input(answer3) is a number or string, if it is not a number it should return "Enter Numbers Only" but it returns the same even for numbers. Please suggest me a solution. #include #include…
Rukshan Mahendra
  • 113
  • 1
  • 1
  • 7
8
votes
7 answers

How do you get an unsigned long out of a string?

What's the safest and best way to retrieve an unsigned long from a string in C++? I know of a number of possible methods. First, converting a signed long taken from atol. char *myStr; // Initalized to some value somehow. unsigned long n =…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
7
votes
2 answers

Convert string to integer type T, checking for overflow

The goal is a function which, given a string containing a number, and an integral type T, returns success + converted value if the value fits in the type without overflow, or failure otherwise. Using std::istringstream to read a number from a string…
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
7
votes
2 answers

Iterate through parts of a string

I have string in the form [3339:1.6101369,1062:1.5,5751:1.5,6376:1.5, ... ] I want to iterate through the comma separated key-value pairs. What is the best or shortest way to do this?
Thomas
  • 10,289
  • 13
  • 39
  • 55
7
votes
3 answers

Convert string to double or float c#

I need to convert a string to double. Or float, whatever suits best for this type of conversion. The string is "25.00". How would I convert this string so that I can use it in calculations? I've tried with: string s1 = "2"; string s2 =…
Kim Andersson
  • 251
  • 2
  • 4
  • 15
7
votes
4 answers

Optimizing several million char* to string conversions

I have an application that needs to take in several million char*'s as an input parameter (typically strings less than 512 characters (in unicode)), and convert and store them as .net strings. It turning out to be a real bottleneck in the…
greggorob64
  • 2,487
  • 2
  • 27
  • 55
1
2
3
25 26