this is probably to many steps to accomplish this and if there is a better way then i am all ears. anyway its not working as expected.
I am reading 120 bytes starting from posistion x to x+120 to a Byte array. Then i am copying that Byte array to a Char array, at this step i am losing some of that data. I am then converting that Char array to a String*. What am i doing wrong here
BinaryReader* FileReader = "new File to open"
Byte bBuf[] =
new Byte[120];bBuf =
FileReader->ReadBytes(120);Char cBuf[] =
new Char[120];bBuf->Copy(bBuf, cBuf, 120);
String* sTemp =
new String(cBuf);
byte array to char array
Donald Baker
woodsmith
Strings are NULL terminated in Windows and .NET. So if you copy a byte array to a String, and the Byte array is supposed to represent a string, but is not zero-terminated, the copy will fail, if you use any string copy mechanism that expects a null-terminator.
If you have a non-null terminated array, treat it as a byte array as it should.
jung1975
i might have taken to many steps to make this work but now it does. here is a code snippet of how i did it
String* sBuffer =
new String(S""); sFile->set_Position(iOffset*x);bBuf =
this->m_pMMDM->sFileReader->ReadBytes(120); for(int i=0; i<bBuf->get_Count(); i++){
Char cTemp = Convert::ToChar(bBuf
);
if(cTemp == ' ' || cTemp == NULL)sBuffer = String::Concat(sBuffer, Convert::ToString(S
".")); elsesBuffer = String::Concat(sBuffer, Convert::ToString(Convert::ToChar(bBuf
)));
}
arcdigital
What are all those zeroes at position bBuf[5] and bBuf
then As far as the debugger's concerned, that's a null terminated array. And have you checked the string that Encoding::GetString returned using Console::Writeline
Jason Fan
here is the data for each byte element up till the conversion stops, this is what the debugger tells me, left side of the equals is the name and right side of equals is the value straight from the Autos in the debugger
bBuf[0] = 67 'C'
bBuf[1] = 53 '5'
bBuf[2] = 46 '.'
bBuf[3] = 52 '4'
bBuf[4] = 48 '0'
bBuf[5] = 0 ''
bBuf
= 0 ''
bBuf[7] = 32 ' '
The converted string shows only the first 5 bytes, it stops at bBuf[5]
ididntdoit
Najmeh
In addition to what nish said, have you actually checked the output using Console::WriteLine I don't trust the debugger to properly display strings with null terminators (it can't even handle nulls properly for native code let alone managed code).
What you may be able to do is replace each instance of 00 with another character (like '_'). It may help you to visualise the data.
jakob aagesen
Assuming that you have to use BinaryReader (have you considered using StreamReader and StreamReader::ReadToEnd() instead )... to convert Bytes to chars, you Must tell .NET what encoding the bytes are in. Are they ASCII sequences, UTF-16 sequences, UTF-8 sequences, big-endian sequences, or some other encoding
Once you have determined the type of encoding, you can now convert the bytes to chars using:
System::Text::Encoding ^encoder = System::Text::Encoding::Unicode;
String ^sTemp = encoder->GetString(bBuf, x, x+120);
(assumes the sequence is UTF-16).
Richard Bartel - MSFT
this actually worked
Encoding* encoder = Encoding::ASCII;
String* sTemp = encoder->GetString(bBuf);
BUT it is still truncating some of the data, actually a lot of it is being removed. The Byte array is of size 120, when i debug it i can see that each element in the array has data but during the conversion some of that data is removed.