Need to convert 4 byte returned from socket call to integer

J

Jim

Here is a snipet of the code.

// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
=====================================================
StateObject state = (StateObject) ar.AsyncState;

When Data returns from socket
state.sb.ToString(); Contains "0\0\0\0" This is the 32 bit win32
error code
this is Success code.
state.sb.ToString(); Contains "\0\0\0" This is an Error where 
is value of 2 ( #define ERROR_FILE_NOT_FOUND 2L )


How do I convert the bytes stored in the string to an int32?
 
R

Roy Fine

Jim

assuming that your string variable is constructed as you indicated -
consider this approach:

/* *********************** */
string errstr = Encoding.ASCII.GetString(new byte[] {2,0,0,0});

byte[] bytearray = Encoding.ASCII.GetBytes(errstr);
int errc = BitConverter.ToInt32(bytearray,0);

Console.WriteLine("Error Code: {0}",errc.ToString("0000"));
/* *********************** */


regards
roy fine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top