3 Replies - 702 Views - Last Post: 17 June 2013 - 07:00 AM Rate Topic: -----

#1 shat_ismail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-June 13

Error implicity convert type 'byte[ ] to int'

Posted 16 June 2013 - 10:24 AM

this is my code to create server receive byte from client, the problem is I want to use swicth case. As you know, switch can only process char or int, can anyone tell me how to make server covert the receive byte to int or char. Please help me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using InTheHand;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Ports;
using InTheHand.Net.Sockets;
using System.IO;




namespace CAN_Server
{
    public partial class CAN_Server : Form
    {
        
        public CAN_Server()
        {
           
      
            InitializeComponent();
        }

        private void btServer_Click(object sender, EventArgs e)
        {
            if (serverStarted)
            {
                updateUI("Server already started");
                return;
          
            }
             else
            {
              connectServer();
            }
        }
              private void connectServer()
            {
            Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
            bluetoothServerThread.Start();
            }
         Guid mUUID = new Guid("5E1AB990-D600-11E2-8B8B-0800200C9A66");
        bool serverStarted = false;

         private void ServerConnectThread()
         {
            serverStarted = true;
            updateUI("Server started, waiting for client");
            BluetoothListener blueListener = new BluetoothListener(mUUID);
            blueListener.Start();
            BluetoothClient comm = blueListener.AcceptBluetoothClient();
            updateUI("Client has Connected");

             Stream mStream = comm.GetStream();
             while (true)
             {
                 try
                 { 

                    byte[] received = new byte[1024];
                    mStream.Read(received, 0, received.Length);
                    updateUI("Received: " + Encoding.ASCII.GetString(received));


                    int Cir;
                    Cir = received; //problem occur here cannot use the receive to determine Cir value

                    switch (Cir)
                    {
                        case 1 :
                            serialPort1.PortName = "COM5";
                            serialPort1.BaudRate = 9600;
                            serialPort1.DataBits = 8;
                            serialPort1.Parity = System.IO.Ports.Parity.None;
                            serialPort1.StopBits = System.IO.Ports.StopBits.One;
                            serialPort1.Open();
                            serialPort1.Write("a");
                            serialPort1.Close();
                            break;
                        case 2:
                            serialPort1.PortName = "COM5";
                            serialPort1.BaudRate = 9600;
                            serialPort1.DataBits = 8;
                            serialPort1.Parity = System.IO.Ports.Parity.None;
                            serialPort1.StopBits = System.IO.Ports.StopBits.One;
                            serialPort1.Open();
                            serialPort1.Write("a");
                            serialPort1.Close();
                            break;
     

                    }
   
                 }
                   catch (IOException exception)
                 {
                        updateUI("Client has disconnected");
                   }
             }

            

         }
         private void updateUI(string message)
         {
             Func<int> del = delegate()
             {
                    tbOutput.AppendText(message + System.Environment.NewLine);
                return 0;
             };
             Invoke(del);

         }

         
    }

}

This post has been edited by tlhIn`toq: 16 June 2013 - 11:00 AM
Reason for edit:: Size of font fixed : Don't scream at people


Is This A Good Question/Topic? 0
  • +

Replies To: Error implicity convert type 'byte[ ] to int'

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3237
  • View blog
  • Posts: 10,868
  • Joined: 12-December 12

Re: Error implicity convert type 'byte[ ] to int'

Posted 16 June 2013 - 10:57 AM

Wow! Big text!

This MSDN page discusses how to use the BitConverter class to convert a byte array into ints (and back again), and provides other links.

I did notice, however, that there were MANY links when I Googled "C# convert byte to int" :whistling:. Did you try this?

This post has been edited by andrewsw: 16 June 2013 - 10:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,666
  • Joined: 02-June 10

Re: Error implicity convert type 'byte[ ] to int'

Posted 16 June 2013 - 11:02 AM

Quote

As you know, switch can only process char or int,

You should re-read about the switch operator as this is not true.
Try some experimentation with other types and you'll see.
Was This Post Helpful? 2
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Error implicity convert type 'byte[ ] to int'

Posted 17 June 2013 - 07:00 AM

You also have to consider your logic. You're receiving a byte array. What's in that array? At one point, you convert it to ASCII and print it. Does that mean it's a string? How can you directly convert from a byte stream to one single int? Well, you can't, unless the data you receive is representing an integer.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1