How can I convert a binary string to an integer using RTL and C#?

I’ve been reading the documentation for the Convert class in RTL, hoping to see the .NET Convert.ToInt64(string s, int base) method but I can’t find an equivalent.

Can anyone suggest a cross-platform way to take a binary string (e.g: 00110) and convert it to an integer?

I don’t think we have one; I’ll look at adding a (Try)ToInt64 overload that takes a base, as well.

I’ve pushed a native Convert.TryBinaryStringToUInt64 now. Only did minimal testing, but these succeed so I’m fairly;y confident its good:

    method TestBinary;
      Check.AreEqual(Convert.TryBinaryStringToUInt64("1x"), nil);
      Check.AreEqual(Convert.TryBinaryStringToUInt64("1"), 1);
      Check.AreEqual(Convert.TryBinaryStringToUInt64("10"), 2);
      Check.AreEqual(Convert.TryBinaryStringToUInt64("100"), 4);
      Check.AreEqual(Convert.TryBinaryStringToUInt64("1001"), 9);


1 Like

Brilliant! Thanks @mh.

1 Like

Just realised that this method hasn’t been added to the docs page:

I’ll make sure we run an update this week; these docs are generated automatically from the binaries metadata.

1 Like