Convert Int to String

Java - Convert integer to string

There are multiple ways:

  • String.valueOf(number) (my preference)
  • "" + number (I don't know how the compiler handles it, perhaps it is as efficient as the above)
  • Integer.toString(number)

Easiest way to convert int to string in C++

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

#include <string> 

std::string s = std::to_string(42);

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

auto s = std::to_string(42);

Note: see [string.conversions] (21.5 in n3242)

Dart convert int variable to string

Use toString and/or toRadixString

  int intValue = 1;
String stringValue = intValue.toString();
String hexValue = intValue.toRadixString(16);

or, as in the commment

  String anotherValue = 'the value is $intValue';

Converting an integer to a string in PHP

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";

converting Integer to String process ( under the hood )

String to integer

Let's start with converting a string to an int, as I think it's a bit simpler to think through. I'll make a few assumptions to start:

  1. our int method will only deal with int inputs, no floats, complex numbers, etc. for now.
  2. we will only deal with positive numbers for now as well.
  3. I won't be dealing with intentionally wrong inputs, like int("Wassup")

The implementation will go through the string input from right to left, and build up the integer number by number.

def custom_int(input):
# Your intuition is right that we will need some kind of look up! this matches a character to a number
s_to_i_dict= {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

# imagine our input is the string "123", we want to build up the number 123
# one digit at a time. If you break it down, 123 is equal to 100 + 20 + 3
# Thinking it through further, that's the same as 1*100 + 2*10 + 3*1

# So for each digit going right to left, we'll multiply it by some multiplier
# add it to our result, theb change that multiplier to handle the next digit.

multiplier = 1
output = 0

# This is a little shortcut to get you looping through a list or a string from the last element to the first:
for digit in input[::-1]:
# digit is a character, we find the corresponding number, multiply it by the multiplier, then add it to the old version of output
output = output + ( s_to_i_dict[digit] * multiplier)

# we are done with this digit, so the next multiplier should be 10 times the last (going from digits to tens to hundreds etc.)
multiplier = multiplier * 10
return output

Running this you'd get:

s_to_i("123")

123

type(s_to_i("123"))

<class 'int'>

For the "123" input, our loop will run 3 times. the first time around output will just be the rightmost digit 3, the next time around, we will add 2*10 to output, giving us 23.

The final time through the loop we will get 23 + 1*100, or 123.

Integer to string

We can apply the exact same pattern to the int to string conversion. The same assumptions apply as I won't cover all edge cases, but fundamentally we will do the same thing: go through the numbers from right to left, then build up a string representation of the number.

Now we can't loop over numbers as easily as we loop over strings, but with some good use of mod and division we can get a similar pattern. say n = 123, how do we get just the rightmost digit from the number? Well the rightmost digit is the remainder of dividing n by 10, or in code rightmost_digit = n % 10.

Once we have the rightmost digit, the next step is to try to extract the second rightmost digit, 2 in this case. There are a few ways to do that but my favorite is to recognize that we have already grabbed the rightmost digit, so we don't need it anymore

We can update our number n as follows: n = n // 10 which will give n the value of 12 instead of 123. This is called integer division, and is basically primary school division before you discovered floats :P

How does this help? well notice that 2 is the rightmost digit of 12 and we already know how to grab that! Let's put this all together in a function.

def i_to_s(input):
# Notice that this is the opposite dictionary than before. ints are the key, and they give us the character we need.
i_to_s_dict={0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}

# This time we want our output to be a string so we initialize an empty string
output = ""

# There are more precise ways to set up this loop, as this one creates an edge case I'll leave up to you to find, but works with _almost_ every integer :P
while(input != 0):

rightmost_digit = input % 10

# We concatenate the new character we found with the output we've accumulated so far
output = i_to_s(rightmost_digit) + output

# Change the initial number for the next iteration
input = input // 10

return output

i_to_s(123)

'123'

type(i_to_s(123))

<class 'str'>

Convert string to int in C# (When String is 'E0305' To convert Int is not Work)

If you just want to skip invalid string value, it is better to use TryParse instead of returning 0 (which might be valid value). At your calling code it should look like this:

string val = "F0005";
if (int.TryParse(val, out int i) {
// parse success. you can use i here
}
else {
// parse failed.
}

If you really want it to be 0, this should work

string val = "F0005";
int i = int.TryParse(val, out int x) ? x : 0;

Int to string in go?

You can use strconv.Itoa (or strconv.FormatInt if performance is critical) by simply converting the int16 to an int or int64, for example (Go Playground):

x := uint16(123)
strconv.Itoa(int(x)) // => "123"
strconv.FormatInt(int64(x), 10) // => "123"

Note that strconv.FormatInt(...) may be slightly faster according to a simple benchmark:

// itoa_test.go
package main

import (
"strconv"
"testing"
)

const x = int16(123)

func Benchmark_Itoa(b *testing.B) {
for i := 0; i < b.N; i++ {
strconv.Itoa(int(x))
}
}

func Benchmark_FormatInt(b *testing.B) {
for i := 0; i < b.N; i++ {
strconv.FormatInt(int64(x), 10)
}
}

Run as $ go test -bench=. ./itoa_test.go:

goos: darwin
goarch: amd64
Benchmark_Itoa-8 50000000 30.3 ns/op
Benchmark_FormatInt-8 50000000 27.8 ns/op
PASS
ok command-line-arguments 2.976s

Convert int to string?

string myString = myInt.ToString();


Related Topics



Leave a reply



Submit