Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

[SOLVED] WriteString Problem

[SOLVED] WriteString Problem

Anonymous
Not applicable
406 Views
2 Replies
Message 1 of 3

[SOLVED] WriteString Problem

Anonymous
Not applicable
MS irritating again, the problem is that whenever i try to write a string MS also writes a blank byte too. For example if i say
WriteString fileWriter  "C"
WriteString fileWriter "D"
WriteString fileWriter "E"

then it will write like this
TEXT          HEX
C.D.E. 4300 4400 4500

i dunno why its writing 00 too. here is my fileWriter declaration
fileWriter = fopen filePath "wb"


any idea guys ?
0 Likes
407 Views
2 Replies
Replies (2)
Message 2 of 3

Steve_Curley
Mentor
Mentor
You're using a binary file. In C/C++ (which is what Max is written in AFAIK) strings are terminated (delimited) by a null character (0x00) - I suspect this is being carried forward into the writing of a string to a binary file. It's logical for it to do that otherwise how could it read back the same string if it doesn't know where the string ends?

I can't answer the second part of your question because you haven't provided the code for it, but it's probably to do with the way numbers are stored. Your value of "44" is probably being interpreted as a WORD rather than a BYTE, thus it is really "0044". Problem is that Intel based systems store number reversed ie. LSB MSB (least significant byte, most significant byte) so your number becomes "4400" when written to the file. If you want a headache http://en.wikipedia.org/wiki/Endianness

If you're trying to write out a text file, try using a FileStream rather than a binary file.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 3

Anonymous
Not applicable
yes its in binary coz its about game. You are right about terminating character (0x00) since it make sense. Dude i wrote the code that was writing the string i dunno why you need more ? this is same in C# but i convert string to char array then write its ascii values as byte. Therefore, is there anyway in MS that can convert char to byte mean can return ascii value or i should make use a .dll with that function and use it. I think this is better idea ?

Edited :
ok, i did, do not have time to wait for another solution 🙂
here is code if anyone need for dll
namespace CustomMSfunctions
{
public class MSfunctions
{
public static byte ToByte(string value)
{
return (byte)(Convert.ToChar(value));
}
public static byte[] ToBytes(string values)
{
char[] tmp_char = values.ToCharArray();
byte[] output_bytes = new byte;
for (int a = 0; a < output_bytes.Length; a++)
output_bytes = (byte)tmp_char;
return output_bytes;
}
}
}


for MS
funcCaller = dotnetobject "CustomMSfunctions.MSfunctions"
newval = funcCaller.ToByte("A") -- for single Char(testing)
format "Single Char To Byte Conversion : %\n" newval
newvalues = funcCaller.ToBytes("ABCD") -- return byte array
print newvalues


edited 2 :
another better solution, i dunno why good ideas come late xD anyway the another way i found is that whenever write a string then right after step one byte back and then that 00 will be replace with next strings or any value.
0 Likes