Emgucv how to convert a type

How to convert int to a string in C ++

  1. Use the stringizing macro to convert an int literal into a character string
  2. Use the method for converting int to string
  3. Use the class and method to convert Int to String
  4. Use the method for converting int to string

This article introduces C ++ ways to convert one to one.

Use the stringizing macro to convert an int literal into a character string

This method is of limited use when converting int to strings. This is because it can only be used when so-called hard-coded numeric values ​​have to be converted into a type.

Macros are blocks of code that the programmer assigns names, and when the name is used it is replaced by macro extension (the right part of the macro statement).

This is a function of the C / C ++ preprocessor, which means that you can only use this with literal values.

Output:

Note, however, that if you want to string the result of another macro expansion (in this example, the one that expands to), you must define a two-stage macro, as shown in the following code block:

Output:

Use the method for converting int to string

is a built-in library function that takes a single numeric value as an argument and returns an object. This method is the recommended solution to this problem. Note, however, that passing the floating point values ​​to the method produces some unexpected results, as shown in the following code example:

Output:

Use the class and method to convert Int to String

Another alternative to solve this problem is to use the class that stores the instance internally and provides the method to get the object from the contents of.

Output:

Use the method for converting int to string

The following method is relatively cumbersome compared to others because a temporary array must be initialized to store the converted results. Although, on the plus side, this method is locally independent, non-assigning, and non-throwing. The function takes the area of ​​the array and converts the integer into a string. As soon as the characters are stored in the variable, a new object is initialized with the argument.

Related Article - C ++ Int

  • How to convert int to char array in C ++
  • How to convert Int to ASCII characters in C ++