Writing to a Binary File

I have this requirement to export data to a file. The 1st 10 characters in the file has to have information which is used by an external application.

This information is the BYTE value of the date / time + some additional flags from the system.

The requirement is straight forward when the data that is to be converted is non zero, and I can use the num2char function.

The problem i have is when the data that is to be converted is non zero, num2char returns a blank (null?) string. This is not written to the export file.

I tried using BinaryIO to write files, but this creates a file that is not suitable to me as the output file is structured in a particular way, and I believe that BinaryIO creates a file inits own native format.

The only other option will be to use a DLL developed in .Net that will do the same, but I need to use this as a last resort.

I am posting a sample Job below, that will use both BinaryIO, and AsciiIO. The expected output should be a file with exactly 10 characters. However, I never get more that 8 in the Ascii file because of the last two zeros, and some unusable data in the Binary file.

Any suggestions are welcome.

static void ___BinaryExport(Args _args)
{

#file
BinaryIo binaryFile;
AsciiIO asciiFile;
Filename _fileName;

Binary binaryHeader;

System.Byte byteVersion;
System.Byte byteDay;
System.Byte byteMonth;
System.Byte byteYear;
System.Byte byteHour;
System.Byte byteMinute;
System.Byte byteSecond;

System.Byte byteType;
System.Byte byteEncrypted;
System.Byte byteCompressed;

int i;
utcDateTime rightNow = datetimeutil::utcNow();

;

binaryHeader = new Binary(10);

//datetimeutil::day(rightNow);

binaryHeader.byte(0,1); //1 : Header is Always 1

binaryHeader.byte(1,datetimeutil::day(rightNow)); //2 : Day part of Date
binaryHeader.byte(2,datetimeutil::month(rightNow)); //3 : Month part of Date
binaryHeader.byte(3,datetimeutil::Year(rightNow)); //4 : Year part of Date

binaryHeader.byte(4,datetimeutil::hour(rightNow)); //5 : Hour part of Date
binaryHeader.byte(5,datetimeutil::minute(rightNow));//6 : Minute part of Date
binaryHeader.byte(6,datetimeutil::second(rightNow));//7 : Second part of Date

binaryHeader.byte(7,1); //8 : Comes from Parameter

binaryHeader.byte(8,0); //9 : Is Encrypted? 0 for No, 1 for Yes
binaryHeader.byte(9,0); //10: Is Compressed? 0 for No, 1 for Yes

_filename = “C:\Interface\Temp\Bin001.xhd”;
binaryFile = new BinaryIo(_filename, #io_write);
for (i=0; i<=9; i+=1)
{
binaryFile.writeExp([binaryHeader.string(i)]);
//binaryFile.writeExp([binaryHeader.string(i)]);
}

_filename = “C:\Interface\Temp\Asc001.xhd”;
asciiFile = new asciiIo(_filename, #io_write);
for (i=0; i<=9; i+=1)
{
asciiFile.writeChar( binaryHeader.byte(i) );
}

}

I suppose you have made a mistake in your post when in both cases, you say that “the data to be converted is non zero”. I guess you have the problems when the data to be converted is zero. If it’s the case, I think that it is as simple as to check the values you want to write in the file before you do it, or check the output of the num2char function before you write it. If you detect that the output is null or blank, just write the values you want into the file.

Hope it helps.

I stand corrected. I do have an issue when trying to convert a zero to a character.

I still need to convert zeros to characters as well

For example, if i need to send the value 65 to the external app, i will write A to the header (the num2Char value of 65). This way when I i need to send a zero value, i need to convert 0 to the num2Char value of 0, but this is where the issue lies. The value that I want in this case is the byte value of 0, but this does not get written to the file.

I was able to workaround by writing a .Net DLL, but I would prefer to do this directly in AX for easier support.

As far as I have understand, you know currently the value you want to write to the file when you send the value 0, or at least, you’ve done it in .NET. Could you use this solution?

If (value == zero)

Write zero value to file

Else

Write (Num2Char(value))