How to convert int/long/float/double numeric types and strings into each other in Candlestick +
In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Long long_data=-9828
Unsigned char data [4]
Memcpy (data,&t,4); / / save data of type long with 4 char.
Long my_long_data=0
Memcpy (& tt,data,4); / / restore long type data from 4 char.
1. Convert int/long/float/double to string
Method 1:itoa, ltoa (a represents the meaning of array array)
Header file: stdlib.h
Example:
Int a = 3
Long b = 23
Char buf1 [30] = ""
Itoa (a, buf1, 10); / / 10 means decimal, and the content saved by buf1 is "3"
Char buf2 [30] = ""
Ltoa (b, buf2, 10); / / 10 means decimal, and the content saved by buf2 is "32"
Method 2:sprintf
Header file: stdio.h
Example:
Int a = 3
Float b = 4.2f
Char buf [30] = ""
Sprintf (buf, "% djue% f", a, b); / / the content saved by buf is "3djue 4.2", which can be compared with printf
Method 3:ostringstream
Header file: # include
Using namespace std
Example:
Int a = 3
Float b = 4.2f
Ostringstream s1
S1