blah blah blah is here! blah blah » Close

up2down
link

Hi all,

I'm doing a project that involves sending a file to a major corporation. This file needs to contain a "EBCDIC HEX 1C" character as a segment terminator. I've tried using the code below, but apparently it ain't right :-/

Any help will be massively appreciated!

string newline = HexAsciiConvert("1C");

private string HexAsciiConvert(string hex)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= hex.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),
System.Globalization.NumberStyles.HexNumber))));
}
return sb.ToString();
}

output = yadayada + newline;

File.WriteAllText(sfd.FileName, output);

last answered one year ago

3 answers

link

As psymon25 says the code should work, though you can do it much more easily with:

string newline = "\x1C";
output = yadayada + newline;
File.WriteAllText(sfd.FileName, output);

If it's not working properly, then the problem could be the encoding.

File.WriteAllText() uses UTF-8 by default which will use 1 byte for characters in the range 0 - 127 but 2 bytes for characters in the range 128 - 255. Also a byte order mark is not written at the beginning of the file.

If the corporation's machine is expecting some form of extended ASCII (say Windows-1252 which uses 1 byte for all characters in the range 0 - 255) and you're sending it, say, some Danish characters then you will have problems.

There's an overload of WriteAllText() which takes an Encoding as its third parameter:
File.WriteAllText(sfd.FileName, output, System.Text.Encoding.GetEncoding(1252));

So, if you don't know what it's expecting, you could try that.

EDIT

Actually, it might be expecting the whole file to be encoded as EBCDIC which is code page 500 though there are some international variants (see here).

psymon25
482

agreed as all this is really doing in the current lines of code is writing a string to text file which does not require any conversion. Obviously if you are taking this in via a different format in the live environment then it is possible that conversion may be required...

up2down
link

Not sure if im missing the point but what you have seems to do the trick? are you able to post the output you receive?

as i did the following:

private void button1_Click(object sender, EventArgs e)
{
sfd.ShowDialog();
string newline = "1C";
string output;

HexAsciiConvert(newline);

output = "Yadadada " + newline;
File.WriteAllText(sfd.FileName, output);
}



private string HexAsciiConvert(string hex)
{


StringBuilder sb = new StringBuilder();
for (int i = 0; i <= hex.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),
System.Globalization.NumberStyles.HexNumber))));
}
return sb.ToString();
}


And this created the file test.txt with the following output included in the file: Yadadada 1C

up0down
link

Great tip with the "\x1C" :D

I'm not writing any special characters to the file (if the users are entering danish characters in my application, that's their problem) but I think the EBCDIC format is what I need. I'll give it a try and post back :)

Thank you!!!

foamy
2499

Well it seems the original code WAS correct ^-^ There were other errors in the file regarding the data the users entered and the rep. from the company just kept saying it was missing that HEX 1C char ... thanks for the help :)

Feedback