I'm writing a c# client who need to communicate with a c++ server. I'm trying to find a way to send a string to the server, and i'm stuck, since a char in c# is 2 bytes and in c++ it's 1.
How can I convert my strings to send them as a readable char array for the server?
Thanks a lot!
Ps : I will have the same problem with other types like int and stuff I think.
In C++ you can use std::wstring which is wide character and of two bytes.
You can quite easy convert your string to an ascii byte array before sending it to the server:
string message = ...
byte [] data = Encoding.ASCII.GetBytes(message);
server.Send(data);
Be sure however that you send messsages that consist of characters covered in the ascii-table. Characters outside that table can bring some surprises when converted to ascii.
Converting the received answer from the server back into a string
byte [] received = ...
string response = Encoding.ASCII.GetString(received);
Generally speaking, sending data from client to server and back through some kind of connection is not the easiest thing to do.
I can share my experience, in which I need to serialize properties of serializable classes into stream of bytes to be sent across a generic connection.
Using the System.BitConverter you can get the representation of basic data types (bool, char, double, float, ...) into arrays of bytes:
byte[] f1 = BitConverter.GetBytes(MsgID); // MsgID is a ulong
for string objects you could use the UTF8 encoding:
// == Payload is a C# string ==
// calculates how many bytes we need to stream the Payload
payloadBufferSize = Encoding.UTF8.GetByteCount(Payload);
// create a suitable buffer
payloadBuffer = new byte[payloadBufferSize];
// Encode the Payload in the buffer
Encoding.UTF8.GetBytes(Payload, 0, Payload.Length, payloadBuffer, 0);
doing so you have an array of bytes you can send through your connection, given that on the other side you have some kind of object that is able to decode an UTF8 stream of bytes.
If you just want to get a plain ASCII stream, you could use the Encoding.ASCII encoder instead of the Encoding.UTF8 in the sample above, but if you have unicode character you'll get a '?' as a resulting char.
Related
I am making a audio chat program, So I tried to sending Audio bytes via web-socket
first I did, I get audio bytes and send it on But it was failed(maybe there can't pass completely)
second I tried is convert bytes to string with use BitConverter and convert again to byte array with Encoding.UTF8.GetBytes method
this is my code
var pcmAudio = stream.ToByteArray();
var audio = Encoding.UTF8.GetBytes(BitConverter.ToString(pcmAudio));
if I send that 'audio' it works. I can convert to byte array and I can play audio.
but, if I send pcmAudio there are an error
Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
above is my receiving audio code. data is string. there's no way to receive with Byte type.
So i had to convert data to bytes.
Unfortunately it doesn't work.
the error message is 'Wave header is corrupted'
i want to send byte array compeletly.
ur question 1. Why do you want to send bytes ? You know the way to send audio with bitconverter
my answer 1. Byte length would be larger than not converted
thank you
I would like to get the number of bytes in a string using UTF-8 encoding without explicitly creating an array of bytes (because I do not need to use the array, just the number of bytes). Is this possible? My question is almost exactly this one but with C# instead of Java.
Thanks!
You can use the method GetByteCount to get the number of bytes that the string would produce with a given encoding.
var byteCount = System.Text.Encoding.UTF8.GetByteCount("myString");
I am reading data from a Telnet server with TcpClient as a byte array and convert into string with ASCII encoder, i.e.:
int size;
byte[] buf;
tcpclient.Read(buf,0,MAX_BUFFER_SIZE);
string resp = new System.Text.ASCIIEncoding().GetString(fullBuffer, 0, fullBuffer.Length);
and have buf with the read bytes. I want to extract (and answer later) IAC commands inside buf, not to have them in resp. Do you have an efficient suggested way.
What first comes to my mind is: Find "\0xFF" strings in resp (a) if followed by "\0xFF", replace "\0xFF\0xFF" with "\0xFF" (b) if not extract followed byte as IAC and answer. resp.Remove() two characters.
Is that an efficient solution? Do you suggest to solve it in byte[] level (is there a way to have some values in byte[] that are ignored by ASCII encoder?) or after encoding into string?
What first comes to my mind is: Find "\0xFF" strings in resp (a) if followed by "\0xFF", replace "\0xFF\0xFF" with "\0xFF" (b) if not extract followed byte as IAC and answer. resp.Remove() two characters.
Part a will work, but part b is lacking. There are several commands that consist of more than 2 bytes. You're going to have to do more work and determine how many bytes each command should consume. For instance, the SB command will contain a variable number of bytes depending on what the suboption is.
C#: How should TCP socket buffer data be handled? As bytes or converted to an ascii string?
I am using methods that are involved in parsing specific data from the returned tcp socket buffer, which practice would be best?
Should I process/parse my data in it's raw byte form? Or should I process it after convert it to an ascii string since string datatypes have niftier text operations?
In general, as bytes. It's basically binary data - it's up to the protocol above TCP that to interpret that binary data appropriately.
Now, what is your data? Are you in control of the protocol? If so, is it meant to be text data? Converting binary data for an image (for example) into ASCII is likely to be disastrous... but if it's a genuinely ASCII-only protocol, it's probably the right way to go.
If you don't know the protocol, don't perform any conversions: they may well lose information unless you're very careful (e.g. using base64 instead of just an ASCII encoding).
If you do know the protocol, that should dictate how you treat the data.
Actually the buufer data is aways "raw" bytes, so if you want to parse the content you'll have to convert the byte[] into a string. For exemple:
string myString = Encoding.ASCII.GetString(myBufferData);
I think it depends on the data being contained in the buffer. If it's a string it's convenient to convert the buffer to a string.
Also note, that you should prepend some header data before you send the string. Like length of the data payload, checksum/parity etc. Network is a non-deterministic environment, and you can't say for sure who sends what on a specified port, you might get a hard-to-trace crashes if you just convert the received buffer directly into a string.
I am working on C#, trying below code
byte[] buffer = new byte[str.Length];
buffer = Encoding.UTF8.GetBytes(str);
In str I've got lengthy data but I've got problem in getting complete encoded bytes.
Please tell me what's going wrong and how can I overcome this problem?
Why are you creating a new byte array and then ignoring it? The value of buffer before the call to GetBytes is being replaced with a reference to a new byte array returned by GetBytes.
However, you shouldn't expect the UTF-8 encoded version of a string to be the same length in bytes as the original string's length in characters, unless it's all ASCII. Any character over U+007F takes up at least 2 bytes.
What's the bigger picture here? What are you trying to achieve, and why does the length of the byte array matter to you?
The proper use is:
byte[] buffer = Encoding.UTF8.GetBytes(str);
In general, you should not make any assumptions about length/size/count when working with encoding, bytes and chars/strings. Let the Encoding objects do their work and then query the resulting objects for that info.
Having said that, I don't believe there is an inherent length restriction for the encoding classes. I have several production apps doing the same work in the opposite direction (bytes encoded to chars) which are processing byte arrays in the 10s of megabytes.