C++ equivalent to C# Encoding.ASCII.GetString() - c#

I have a UDP socket in C# that sends a message using the following code
newsock.Send(sendBuffer, sendBuffer.Length, sender);
where sendBuffer is a byte[] data type.
How can I convert this message when I receive it in a UDP socket written in C++?
The C++ code is the following:
recvfrom(socket_desc, client_message, sizeof(client_message), 0,(struct sockaddr*)&client_addr, &client_struct_length)

You already have the raw bytes in client_message, and recvfrom() will return the message's byte length. If the data is truly ASCII then just use the data as-is as a char string, there is no need to convert it.
But, if you need a C++ std::string, it has a constructor that takes a char* and length as input parameters.
On the other hand, if you really need a Unicode string (ie what Encoding.GetString() returns), you can use the Win32 MultiByteToWideChar() function, or equivalent, specifying ASCII as the charset to convert from.

Related

Python to C#: how to format data for sockets?

I am translating a python communication library into C#, and am having trouble interpreting how the string gets formatted before being sent over tcp.
The relevant portion of the code is as follows:
struct.pack(
'!HHBH'+str(var_name_len)+'s',
self.msg_id,
req_len,
flag,
var_name_len,
self.varname
)
Then it gets sent with: sendall()
I have looked at the Python documentation (https://docs.python.org/2/library/struct.html) but am still drawing a blank regarding the first line: '!HHBH'+str(var_name_len)+'s', I understand this is where the formatting is set, but what it is being formatted to is beyond me.
The python code that I am translating can be found at the following link:
https://github.com/linuxsand/py_openshowvar/blob/master/py_openshowvar.py
Any python and C# vigilantes out there that can help me build this bridge?
Edit: Based on jas' answer, I have written the following c# struct:
public struct messageFormat
{
ushort messageId;
ushort reqLength;
char functionType;
ushort varLengthHex;
string varname;
...
Once I populate it, I will need to send it over TCP. I have an open socket, but need to convert to to a byte[] I assume so I can use socket.send(byte[])?
Thanks
What's being formatted are the five arguments following the format string. Each argument has a corresponding element in the format string.
For the sake of the explanation, let's assume that var_name_len has the value 12 (presumably because var_name is a string of length 12 in this hypothetical case).
So the format string will be
!HHBH12s
Breaking that down according to the docs:
! Big-ending byte ordering will be used
H self.msg_id will be packed as a two-byte unsigned short
H req_len will be packed as above
B flag will be packed as a one-byte unsigned char
H var_name_len will be packed as a two-byte unsigned short
12s self.varname will be packed as a 12-byte string

Php Byte Array Packet

I have a c# library which practically starts listening on a tcpip server an accepts a buffer of a certain size.
I need to send this packet as Byte array from php over the socket in a form of byte array or equivalent.
The packet is constructed for example byte[1] (a flag) is a number from 0 to 255 and byte[6] to byte[11] contains a float number in a string fromat for example:
005.70 which takes 6 bytes representing every character.
I managed to send the flag but when i try to send the float number it does not convert on the other side (C#).
So my question how can i send a byte array to c# using php?
From the C# part the conversion is being handled as follows:
float.Parse(System.Text.Encoding.Default.GetString(Data, 6, 6));
Just after i have posted the question i have dictated my answer. I am not 100% sure if this is the right way but it managed to convert correctly.
Here is the answer:
I created an array of characters and escaped the flag (4) to be the actual byte value being (4) but i didn't escape the money value
$string = array (0=>"\0", 1=>"\4", 2=>"\0", 3=>"\0", 4=>"\0", 5=>"\0", 6=>"5", 7=>".", 8=>"7", 9=>"\0", 10=>"\0");
Imploded all together with nothing as glue:
$arrByte = implode("", $string);
and sent over the opened socket:
$success = #fwrite($this->socket, $arrByte);

Socket message header building

I'm working on a protocol which will transfer block of xml data via tcp socket. Now say I need to read all the bytes from a xml file and build a memory buffer. Then before sending the actual data bytes I need to send one header to other peer end. Say my protocol using below header type.
MessageID=100,Size=232,CRC=190
string strHeader = "100,232,190"
Now I would like to know how can I make this header length fixed (fixed header length is required for other peer to identify it as a header) for any amount of xml data. Currently say I'm having a xml file sized 283637bytes, so the message header will look like.
string strHeader = "100,283637,190"
How can I make it generic for any size of data? The code is being written both in c++ and c#.
There are a number of ways to do it.
Fixed Length
You can pad the numbers numbers with leading zeroes so you know exactly what length of the text you need to work with. 000100,000232,000190
Use Bytes instead of strings
If you are using integers, you can read the bytes as integers instead of manipulating the string. Look into the BinaryReader class. If needing to do this on the C++ side, the concept is still the same. I am sure there many ways to convert 4 bytes into an int.
Specify the length at the beginning
Usually when working with dynamic length strings. There is an indicator of how many bytes need to be read in order to get the entire string. You could specify the first 4 bytes of your message as the length of your string and then read up to that point.
The best approach for you is to implement this as a struct like
struct typedef _msg_hdr {
int messageID;
int size;
int crc;
}msg_hdr;
This will always have 12 bytes length. Now when sending your message, first send header to the receiver. The receiver should receive it in the same structure. This is the best and easiest way

Read byte array byte by byte using C#

I have a byte array and I want to read this array byte by byte and displayed each byte as integer.
how to do this using C#?
Given that the array is called bytes:
foreach(var b in bytes)
{
Console.WriteLine((int)b);
}
Though, in all fairness, the cast to int is probably unnecessary for display purposes.
Some of the C# code I have been writing communicates via TCP/IP with legacy C++ applications. Some codes use a raw packet format where C/C++ structures are passed back and forward.
Example of what the legacy code could look like:
Best Practice Mapping a byte array to a structure in C#

How can I use C# to retrieve Java data from a socket?

I want to send BigInteger data in socket and my friend wants to retrieve the data.
I am using Java, but my friend uses C#.
String str = "Hello";
BigInteger big = new BigInteger(str.getBytes);
byteToBeSent[] = big.toByteArray();
I am sending this byte array (byteToBeSent[]) through socket. And my friend wants to retrieve "Hello".
How is this possible?
From Java, send your string using String.getBytes(encoding) and specify the encoding to match how your friend will read it (e.g. UTF-8).
This will translate your string into a byte stream that will be translatable at the C# end due to the fact that you're both agreeing on the encoding mechanism.
I'm not sure what your BigInteger mechanism is doing, but I don't believe it'll be portable, nor handle sizable strings.
Honestly, your best bet would to be use the built in Encoding classes in C#.
string str = "Hello";
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
And then send that through the socket.
Why do you use BigInteger to send String data? Or is that just an example?
If you want to send String data, use String.getBytes(String encoding), send the result and decode it using System.Text.Encoding.
You'll need to get a custom BigInteger class for C#.
But parsing "Hello" as a biginteger isn't going to work. I you want to send text, you're better of using Navaars method.
On the C# end, you can use System.Text.Encoding.ASCII.GetString(bytesToConvert[]) to convert the received byte array back to a string.
I had thought that was some sort of Java idiom to convert a string to a byte array. It does correctly convert the string into ASCII bytes, and since BigInteger length is arbitrary, the length of the string should not be an issue.

Categories

Resources