I want to send a string through a socket.
So, i convert string into array of bytes.

System::String ^st = "ÿÿÿÿdetails\x00";
array<Byte>^ msg = Encoding::ASCII->GetBytes(st);

And when i convert back the array bytes to a string , i get a different string.

System::String ^t = Encoding::ASCII->GetString(msg);
MessageBox::Show(t,"asd");

The string t is "????details". Can some one please help me . I want to know how to convert the string into array of bytes . Thank You

Dani AI

Generated

Short answer: the leading high-byte characters (U+00FF / 0xFF) are being lost by the ASCII encoder. ASCII only maps characters 0x00–0x7F; characters above 0x7F are replaced by the encoder fallback (usually '?'), so when you round-trip with ASCII you get "????details". This is an encoding issue, not an endianness/byte-order problem.

If those 0xFF values are meant to be raw bytes (a binary header), do not stuff them into a System::String. Build and send a byte array directly and append the text portion encoded with a chosen text encoding (for example UTF-8):

array<System::Byte>^ msg = gcnew array<System::Byte>(/* length */);
// set header bytes explicitly
msg[0] = 0xFF; msg[1] = 0xFF; msg[2] = 0xFF; msg[3] = 0xFF;
// append text bytes encoded as UTF8
auto utf8 = System::Text::Encoding::UTF8;
array<System::Byte>^ textBytes = utf8->GetBytes("details");
Array::Copy(textBytes, 0, msg, 4, textBytes->Length);

If the leading characters are intended to be text and you want a 1:1 byte->char mapping for values 0x80–0xFF, use ISO-8859-1 (Latin1) which preserves 0xFF <-> U+00FF:

auto latin1 = System::Text::Encoding::GetEncoding(28591);
array<System::Byte>^ b = latin1->GetBytes(yourString);
System::String^ s2 = latin1->GetString(b);

To detect data loss when using ASCII, enable an exception fallback (EncoderExceptionFallback) so you get a clear error instead of silent '?' substitutions.

Relevant docs: ASCIIEncoding and Encoding.GetEncoding.

Notes tied to earlier replies: 's intuition about binary-layout issues is worth checking in some contexts, but the visible "?" characters point to an encoding fallback rather than endianness. and : the fix is straightforward in managed code once you pick whether the data is binary or textual and choose the appropriate encoding/representation.

Recommended Answers

All 6 Replies

Quick, before the mods get here! Add the code tags !

Quick, before the mods get here! Add the code tags !

Its not really worth the effort :)

It could be byte order problem.

anyone? help please ! :(

anyone? help please ! :(

sorry, I haven't done managed code.

Help please !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.