I have about 60 lines of Java code I need to convert to C#. This code creates a UDP packet and sends it to a destination IP address on port 1761. I'm not sure what the equivalent C# code is to manipulate the java byte data type and not sure what the Datagram
equivalent is.
This is the Java code I need to translate to C#
private void jBtnReadActionPerformed(ActionEvent e)
{
byte buf[] = new byte[512];
int i = 0;
//1. Check the Buttons to verify that only UDP-API command button is selected
if (true == jRBtnUdpCmd.isSelected())
{
Integer i1 = Integer.valueOf(jTxtCmdId.getText());
int tmp = i1.intValue();
buf[i++] = (byte)((tmp >> 8) & 0xFF);
buf[i++] = (byte)(tmp & 0xFF);
// set Byte 2:0x00 (read request) and byte three: 0x00 (reserved)
buf[i++] = READ_REQUEST; // READ_REQUEST is set to 0x00
buf[i++] = 1; // This byte is reserved.
// buf[i++] = RESERVED; // This byte is reserved.
// convert the space delimited ASCII hex data to byte value
String strData = jTxtCmdData.getText();
int strDataLen = strData.length();
//System.out.println("String: " + strData + " Len: " + strDataLen);
byte[] newBuf = new byte[strDataLen];
int len = convertAsciiHexToByte(strData, strDataLen, newBuf);
// insert 4 bytes of header and copy the remaining bytes here.
for (i=4; i < 4+len; i++)
{
buf[i] = newBuf[i - 4];
}
// Send the IP Data
sendIpData(buf, i);
}
}
private void sendIpData ( byte bData[], int dataLen )
{
if (dataLen > 0)
{
// Display data to send
DisplayOutData(bData, dataLen);
// Build packet with destination Ip address, source & destination
// port numbers
DatagramPacket packet = new DatagramPacket(bData, dataLen, inetAddr, iPortNum);
try
{
dataSocket.send(packet);
}
catch (IOException ioe)
{
System.err.println(ioe);
}
}
}
garrywh
Member
7 Points
36 Posts
Converting Java Code to C#
Nov 21, 2010 06:45 PM|LINK
I have about 60 lines of Java code I need to convert to C#. This code creates a UDP packet and sends it to a destination IP address on port 1761. I'm not sure what the equivalent C# code is to manipulate the java byte data type and not sure what the Datagram equivalent is.
This is the Java code I need to translate to C#
private void jBtnReadActionPerformed(ActionEvent e) { byte buf[] = new byte[512]; int i = 0; //1. Check the Buttons to verify that only UDP-API command button is selected if (true == jRBtnUdpCmd.isSelected()) { Integer i1 = Integer.valueOf(jTxtCmdId.getText()); int tmp = i1.intValue(); buf[i++] = (byte)((tmp >> 8) & 0xFF); buf[i++] = (byte)(tmp & 0xFF); // set Byte 2:0x00 (read request) and byte three: 0x00 (reserved) buf[i++] = READ_REQUEST; // READ_REQUEST is set to 0x00 buf[i++] = 1; // This byte is reserved. // buf[i++] = RESERVED; // This byte is reserved. // convert the space delimited ASCII hex data to byte value String strData = jTxtCmdData.getText(); int strDataLen = strData.length(); //System.out.println("String: " + strData + " Len: " + strDataLen); byte[] newBuf = new byte[strDataLen]; int len = convertAsciiHexToByte(strData, strDataLen, newBuf); // insert 4 bytes of header and copy the remaining bytes here. for (i=4; i < 4+len; i++) { buf[i] = newBuf[i - 4]; } // Send the IP Data sendIpData(buf, i); } } private void sendIpData ( byte bData[], int dataLen ) { if (dataLen > 0) { // Display data to send DisplayOutData(bData, dataLen); // Build packet with destination Ip address, source & destination // port numbers DatagramPacket packet = new DatagramPacket(bData, dataLen, inetAddr, iPortNum); try { dataSocket.send(packet); } catch (IOException ioe) { System.err.println(ioe); } } }What is the C# equivalent of this code?
buf[i++] = (byte)((tmp >> 8) & 0xFF);
buf[i++] = (byte)(tmp & 0xFF);
(Shifting bits and ANDING the operation...)
This is the code I think I need in place of the Java Datagram UDP send packet code
Any help would be appreciated to point me in thre right direction.
Thank you,
Garry
SMAS-SMAS
Contributor
2637 Points
638 Posts
Re: Converting Java Code to C#
Nov 22, 2010 05:25 AM|LINK
Microsoft provide something for conversion process. I never use this so I have no idea, but you can test this !
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=46bea47e-d47f-4349-9b4f-904b0a973174&displaylang=en