473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need to convert 4 byte returned from socket call to integer

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?
Nov 16 '05 #1
1 1596
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
"Jim" <jp***@qg.com> wrote in message
news:d6**************************@posting.google.c om...
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?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
5
by: Bob Homes | last post by:
In VB6, foreground and background colors of controls had to be assigned a single number. If you knew the RGB values for the color, you still had to convert them into the single number accepatable...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
2
by: Gilles Vollant \(MVP\) | last post by:
Hello I've (in WinImage SDK http://www.winimage.com/wima_sdk.htm ) a function that I need to use from a VB.Net apps First, on the unmanaged Win32 DLL, I've a function which get the number of...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.