byte to int conversion error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stephaniee
    New Member
    • Dec 2009
    • 10

    byte to int conversion error

    Hi,

    Im using Visual C# Express Edition 2008. I need to do byte to integer conversion. Here is my codes:

    Code:
    private string ByteToInt(byte[] xyz)
    {
    
    if (BitConverter.IsLittleEndian)
    Array.Reverse(xyz);
    }


    The error is not all code paths return a value. I wasnt sure what the problem lies. I tried to put return xyz. Another error popped up saying "cannot implicity convert type 'byte[]' to 'string'.

    Can anyone help me??

    Thanks.
    Last edited by tlhintoq; Dec 17 '09, 03:37 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Your method is defined to return a string

    private string ByteToInt(byte[] xyz)

    But you aren't returning *a string*.

    Either change your return type to byte[] or convert your byte[] to a string then return that.

    return xyz.ToString()

    Comment

    • stephaniee
      New Member
      • Dec 2009
      • 10

      #3
      Originally posted by tlhintoq
      Your method is defined to return a string

      private string ByteToInt(byte[] xyz)

      But you aren't returning *a string*.

      Either change your return type to byte[] or convert your byte[] to a string then return that.

      return xyz.ToString()
      Thanks. The problem can be solved.

      I have another question

      Here is my code:

      Code:
      public void WriteData(string words)
              {
                          
                          if (!(comPort.IsOpen == true)) comPort.Open();
                        //byte to string
                          byte[] abc = System.Text.Encoding.UTF8.GetBytes(words);
                       // string to byte  
                          words = System.Text.Encoding.UTF8.GetString(abc);
                           
      
                          words = ByteToInt(abc);
                              //send the message to the port
                              comPort.Write(abc, 0, abc.Length);                        
                              DisplayData(MessageType.Outgoing, ByteToInt(abc) + "\n");
      The display message : System.Byte[]. I don't know why the display appeared this way. What had happened? I tried to write words instead of abc. An error occured saying that "cannot convert 'string' to 'byte[]' "
      Last edited by tlhintoq; Dec 17 '09, 04:23 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        I think you should send characters and not bytes to the serial port. They are both 8-bit, but bytes represent arithmetic values.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Its hard to help when I don't know where the problems happen.

            On line 8 you assign to the variable 'words' ... then do nothing with that.
            On line 11 you then re-assing to the variable 'words'

            You don't say which line produces the error

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Its hard to help when I don't know where the problems happen.

              On line 8 you assign to the variable 'words' ... then do nothing with that.
              On line 11 you then re-assing to the variable 'words'

              You don't say which line produces the error

              Comment

              Working...