Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to convert from byte to string. How to do this. I used Serial port RS232 Communication Channel to get the value.

Please suggest me

C#
byte[] buff = new byte[8];
            buff[0] = 1;
            string readValue = "";
            int readcount = 1;
            int offset = 1;

            objport.Write(buff, offset, readcount);
            objport.Read(buff, offset, readcount);
            
            label7.Text = buff[1].ToString();         
            readValue = ByteArrayToString(buff); // here readvalue is 01c2000000000000

            if((readValue.Substring(0,1))=="A")
            {
            }

 public static string ByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return ( hex.ToString());
        }


Thanks in Advance
Sheethal
Posted
Updated 12-Aug-15 3:18am
v3
Comments
CPallini 12-Aug-15 9:19am    
What's wrong with your code?

Pallini

Thanks for the response, my problem is "01c2000000000000" this code as to be convert to actual value,


Thanks
sheethal
 
Share this answer
 
Comments
CPallini 12-Aug-15 10:52am    
And the actual value is?
I mean, the ByteArrayToString function correctly returns the hexadecimal representation of every byte of the array.
Hi palini


thanks for your support now My issue is fixed now. I written the code as below.


Regards
Sheethal


C#
double read1 = 0;
objport.DiscardInBuffer();
byte[] buff = new byte[8];
buff[0] = 1;
string readValue = "";
string[] readValue1 = new string[1];
int readcount = 1;
int offset = 1;
string sWord1 = "", sWord2 = "";
double dResult1 = 0;
double dMulFactor = 0;
double dResult = 0;
//  objport.ReadBufferSize = 0;
objport.Write(buff, offset, readcount);

do
{

}
while (objport.BytesToRead < 2);
byte[] buffer = new byte[objport.ReadBufferSize];
int bytesRead = objport.Read(buffer, 0, 2);
tString = BitConverter.ToString(buffer.ToArray(), 0, 2);
sWord2 = tString.Substring(0, 2);
sWord1 = tString.Substring(3, 2);
int value = Convert.ToInt32(sWord2, 16);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900