byte array to char array

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);



Answer this question

byte array to char array

  • Donald Baker

    i do not have a NULL terminated array. This string is from a file and i want to get only a few bytes of info at different positions in the file.
  • 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.

    Aaron Sulwer wrote:

    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"."));

    else

    sBuffer = String::Concat(sBuffer, Convert::ToString(Convert::ToChar(bBuf)));

    }



  • 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(bBufIdea);

    if(cTemp == ' ' || cTemp == NULL)

    sBuffer = String::Concat(sBuffer, Convert::ToString(S"."));

    else

    sBuffer = String::Concat(sBuffer, Convert::ToString(Convert::ToChar(bBufIdea)));

    }


  • arcdigital

    What are all those zeroes at position bBuf[5] and bBufDevil 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 ''

    bBufDevil = 0 ''

    bBuf[7] = 32 ' '

    The converted string shows only the first 5 bytes, it stops at bBuf[5]


  • ididntdoit

    your right, and no i havent checked the returned string with Console::Writeline
  • 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.


  • byte array to char array