Skip to main content
edited title
Link
Rob Levine
  • 41.5k
  • 13
  • 88
  • 114

C# binary data convertionconversion to string

formatted code
Source Link
Shamim Hafiz - MSFT
  • 22.2k
  • 44
  • 122
  • 182

Here is the deal. I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. What i want is to convert it to a string so i can cut parts of the string and decode them seperately. how can i dot his? The source code is beneath. `using System; using System.IO.Ports; using System.Threading;

public class PortChat { static bool _continue; static SerialPort _serialPort;

using System;
using System.IO.Ports;
using System.Threading;

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 1000;
        _serialPort.WriteTimeout = 1000;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }

    public static void Read()
    {
        while (_continue)
        {
            
            try
            {             
                

                string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }

    public static string SetPortName(string defaultPortName)
    {
        string portName;
              
            portName = "COM6";
       
        return portName;
    }

    public static int SetPortBaudRate(int defaultPortBaudRate)
    {
        string baudRate;

       
        baudRate = "9600";
       
        return int.Parse(baudRate);
    }

    public static Parity SetPortParity(Parity defaultPortParity)
    {
        string parity;

        parity = "None";

        return (Parity)Enum.Parse(typeof(Parity), parity);
    }

    public static int SetPortDataBits(int defaultPortDataBits)
    {
        string dataBits;

        dataBits = "8";

        return int.Parse(dataBits);
    }

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        string stopBits;

        stopBits = "One";

        return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        string handshake;

        handshake = "None";

        return (Handshake)Enum.Parse(typeof(Handshake), handshake);
    }
}

} `

Here is the deal. I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. What i want is to convert it to a string so i can cut parts of the string and decode them seperately. how can i dot his? The source code is beneath. `using System; using System.IO.Ports; using System.Threading;

public class PortChat { static bool _continue; static SerialPort _serialPort;

public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 1000;
    _serialPort.WriteTimeout = 1000;

    _serialPort.Open();
    _continue = true;
    readThread.Start();

    Console.Write("Name: ");
    name = Console.ReadLine();

    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

public static void Read()
{
    while (_continue)
    {
        
        try
        {             
            

            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}

public static string SetPortName(string defaultPortName)
{
    string portName;
          
        portName = "COM6";
   
    return portName;
}

public static int SetPortBaudRate(int defaultPortBaudRate)
{
    string baudRate;

   
    baudRate = "9600";
   
    return int.Parse(baudRate);
}

public static Parity SetPortParity(Parity defaultPortParity)
{
    string parity;

    parity = "None";

    return (Parity)Enum.Parse(typeof(Parity), parity);
}

public static int SetPortDataBits(int defaultPortDataBits)
{
    string dataBits;

    dataBits = "8";

    return int.Parse(dataBits);
}

public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
    string stopBits;

    stopBits = "One";

    return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}

public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
    string handshake;

    handshake = "None";

    return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}

} `

Here is the deal. I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. What i want is to convert it to a string so i can cut parts of the string and decode them seperately. how can i dot his? The source code is beneath.

using System;
using System.IO.Ports;
using System.Threading;

public class PortChat
{
    static bool _continue;
    static SerialPort _serialPort;

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 1000;
        _serialPort.WriteTimeout = 1000;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }

    public static void Read()
    {
        while (_continue)
        {
            
            try
            {             
                

                string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }

    public static string SetPortName(string defaultPortName)
    {
        string portName;
              
            portName = "COM6";
       
        return portName;
    }

    public static int SetPortBaudRate(int defaultPortBaudRate)
    {
        string baudRate;

       
        baudRate = "9600";
       
        return int.Parse(baudRate);
    }

    public static Parity SetPortParity(Parity defaultPortParity)
    {
        string parity;

        parity = "None";

        return (Parity)Enum.Parse(typeof(Parity), parity);
    }

    public static int SetPortDataBits(int defaultPortDataBits)
    {
        string dataBits;

        dataBits = "8";

        return int.Parse(dataBits);
    }

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        string stopBits;

        stopBits = "One";

        return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        string handshake;

        handshake = "None";

        return (Handshake)Enum.Parse(typeof(Handshake), handshake);
    }
}
Source Link
jayt csharp
  • 455
  • 3
  • 8
  • 12

C# binary data convertion to string

Here is the deal. I found a source code and changed it a little bit so i can retrieve data from a receiver that is on com6. The data i am receiving is binary. What i want is to convert it to a string so i can cut parts of the string and decode them seperately. how can i dot his? The source code is beneath. `using System; using System.IO.Ports; using System.Threading;

public class PortChat { static bool _continue; static SerialPort _serialPort;

public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 1000;
    _serialPort.WriteTimeout = 1000;

    _serialPort.Open();
    _continue = true;
    readThread.Start();

    Console.Write("Name: ");
    name = Console.ReadLine();

    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

public static void Read()
{
    while (_continue)
    {
        
        try
        {             
            

            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}

public static string SetPortName(string defaultPortName)
{
    string portName;
          
        portName = "COM6";
   
    return portName;
}

public static int SetPortBaudRate(int defaultPortBaudRate)
{
    string baudRate;

   
    baudRate = "9600";
   
    return int.Parse(baudRate);
}

public static Parity SetPortParity(Parity defaultPortParity)
{
    string parity;

    parity = "None";

    return (Parity)Enum.Parse(typeof(Parity), parity);
}

public static int SetPortDataBits(int defaultPortDataBits)
{
    string dataBits;

    dataBits = "8";

    return int.Parse(dataBits);
}

public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
{
    string stopBits;

    stopBits = "One";

    return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}

public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
{
    string handshake;

    handshake = "None";

    return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}

} `