-4

I'm working on a unity project and need to convert an string input by the user to ASCII binary. To my knowledge this is the way to do that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Binary : MonoBehaviour
{

    public string MatchNumber;
    public string BinaryMatchNumber;
    public TMP_InputField MatchNumberField;
    public Button Submit;

    void Start () {
        Button btnSubmit = Submit.GetComponent<Button>();
        btnSubmit.onClick.AddListener(UpdateMatchNumber);
    }

    void UpdateMatchNumber(string[] args)
    {   MatchNumber = MatchNumberField.text.ToString();
        byte[] asciiBytes = System.Text.Encoding.ASCII.GetBytes(MatchNumber);
        string BinaryMatchNumber = "";
        foreach (byte b in asciiBytes) {
            BinaryMatchNumber += MatchNumber.ToString(b, 2).PadLeft(8, '0');
        }
        Debug.Log(BinaryMatchNumber);
    }
}

Unity console then throws the errors:

Assets\Scripts\Binary.cs(17,39): error CS1503: Argument 1: cannot convert from 'method group' to 'UnityAction'

and

Assets\Scripts\Binary.cs(25,46): error CS1501: No overload for method 'ToString' takes 2 arguments.

I've googled these errors and have been unable to find a solution that works and fixes it. To my understanding, it should just print the text in binary in the console, but it’s clearly not working.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

0

The problem is with this statement: MatchNumber.ToString(b, 2), which is attempting to convert MatchNumber (which is already a string) to a string, using an invalid syntax. No overload for method 'ToString' takes 2 arguments.

padeso
  • 390
  • 1
  • 8
0

I think the error message is quite clear. string does not have any method with the signature ToString(byte, int). It does not have any ToString method with two arguments at all.

If you want to print the string as ascii bytes you could use string.Join

Debug.Log(string.Join(asciiBytes, ", "));

Should print a string like

42, 94, 24 

and so on. Note that it is more common to print bytes as hex values than numerical values.

JonasH
  • 28,608
  • 2
  • 10
  • 23