How can we convert a list to an integer correctly?
{5, 22, 4, 5} -> 52245?
When I use the command FromDigits
in Mathematica
FromDigits[{5, 22, 4, 5}]
The result is incorrect, namely 7245
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityHere are three ways to do it. The first is the best I think:
FromDigits@Flatten[IntegerDigits /@ {5, 22, 4, 5}]
ToExpression@StringJoin[ToString /@ {5, 22, 4, 5}]
ToExpression@StringJoin@StringCases[Characters@ToString@{5, 22, 4, 5}, DigitCharacter]
FromDigits @ ToString @ Row @ {5, 22, 4, 5}
Head[%]
52245 Integer
ToExpression@StringJoin@IntegerString[{5, 22, 4, 5}]
52245
ToExpression@StringJoin@IntegerString[{5, 22, 4, 5},#]&/@{2,10,16}
{10110110100101, 52245, 51645}
{5, 22, 4, 5} ~ StringRiffle ~ "" // FromDigits
52245
list = {5, 22, 4, 5};
FromDigits[StringJoin[IntegerString[list]]]
which gives
52245