No, no function provides the automatic cast. However you can cast with this simple hack (the cast is automatically made by PHP in internal):
float = "1.23"+0;
for generic number:
$yourNumberCasted = $yourStringNumber + 0;
With a function:
function castToNumber($genericStringNumber) {
return $genericStringNumber+0;
}
$yourNumberCasted = castToNumber($yourStringNumber);
Answer from antoox on Stack OverflowHack: Cast of any type to int / float / string is permitted
php - Cast string to either int or float - Stack Overflow
c# - Converting string to integer by selecting the elements to stay - Stack Overflow
Converting an integer to a string in PHP - Stack Overflow
Videos
No, no function provides the automatic cast. However you can cast with this simple hack (the cast is automatically made by PHP in internal):
float = "1.23"+0;
for generic number:
$yourNumberCasted = $yourStringNumber + 0;
With a function:
function castToNumber($genericStringNumber) {
return $genericStringNumber+0;
}
$yourNumberCasted = castToNumber($yourStringNumber);
You can fulfil your requirement using following line.
$i = "1.23";
echo $c = (double)$i; // will return 1.23
$i = "123";
echo $c = (double)$i; // will return 123
This might do the trick for you
string serialcomnum = Regex.Match(serialcom,"\\d+").ToString();
int ints = Convert.ToInt32(serialcomnum);
Another way could be
string serialcom = "$67%!$127a%";
var serialcomnum = Regex.Replace(serialcom, @"[^0-9]+", ",");
serialcomnum = serialcomnum.TrimStart(',').TrimEnd(',');
int[] serialints = serialcomnum.Split(',').Select(int.Parse).ToArray();
Probably a simple Regex is the easiest solution without using other string parsing methods
string input = "$67%!$127a%";
var matches = Regex.Matches(input, @"[0-9]+", RegexOptions.Compiled);
var result = matches.Cast<Match>()
.Select(x => Int32.Parse(x.Value)).ToArray();
foreach (int x in result)
Console.WriteLine(x);
Here the example expects more than one number inside your input string and returns an array of integers. If this is not the case (only one integer expected) then you can replace the ToArray with a SingleOrDefault() and assign the result to an integer variable.
Also, if you have numbers bigger than Int32.MaxValue, then you should change the Int32.Parse with Int64.Parse.
You can use the strval() function to convert a number to a string.
From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.
$var = 5;
// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles
// String concatenation
echo "I'd like ".$var." waffles"; // I'd like 5 waffles
// The two examples above have the same end value...
// ... And so do the two below
// Explicit cast
$items = (string)$var; // $items === "5";
// Function call
$items = strval($var); // $items === "5";
There's many ways to do this.
Two examples:
$str = (string) $int;
$str = "$int";
See the PHP Manual on Types Juggling for more.
You can use sprintf to do it, or maybe snprintf if you have it:
char str[ENOUGH];
sprintf(str, "%d", 42);
Where the number of characters (plus terminating char) in the str can be calculated using:
(int)((ceil(log10(num))+1)*sizeof(char))
As pointed out in a comment, itoa() is not a standard, so better use the sprintf() approach suggested in the rival answer!
You can use the itoa() function to convert your integer value to a string.
Here is an example:
int num = 321;
char snum[5];
// Convert 123 to string [buf]
itoa(num, snum, 10);
// Print our string
printf("%s\n", snum);
If you want to output your structure into a file there isn't any need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.