使用 StreamWriter 的 C# 脚本会创建额外的字符吗? [英] C# Script Using StreamWriter Creates Extra Character?

查看:34
本文介绍了使用 StreamWriter 的 C# 脚本会创建额外的字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SSIS 中使用 C# 脚本任务来输出 ASCII 字符.我这样做是因为我正在创建一个包含压缩字段的文件,一个压缩字段在每个字节中包含两位数字,使用一种称为二进制编码十进制的表示法.

I am using a C# Script Tasks in SSIS to output ASCII characters. I am doing this because I am creating a file with Packed fields, a packed field takes two digits into each byte, using a notation called Binary Coded Decimal.

因此,我发现当输出带有 Œ (0x8C) [Dec 140] 的 NUL (0x00) [Dec 0] 时,它会在它们之间添加一个额外的字符  (0xC2).我不明白为什么会这样.有没有人有任何想法?请看我下面的代码:

So, I have found when outputting a NUL (0x00) [Dec 0] with a Œ (0x8C) [Dec 140] it adds an extra character  (0xC2) between them. I can't figure out why this is happening. Does anyone have any ideas? Please see my code below:

string fileName;
System.IO.StreamWriter writer;

public override void PreExecute()
{
    base.PreExecute();

    this.fileName = this.Variables.FilePath;
}

public override void PostExecute()
{
    base.PostExecute();
    writer.Flush();
    writer.Close();
}

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    writer.Write((char)00);
    writer.Write((char)140);

    writer.Write((char)13);
    writer.Write((char)10);
}

输出如下:

更新我没有指出的一件事是我将十六进制值传递到 C# 脚本中,然后将十六进制值表示的字符写入具有固定长度列的文件.

UPDATE One thing I didn't make a point of is that I am passing Hex Values into the C# Script and then writing the Characters represented by the hex value to a file with fixed length columns.

我不知道这是否有区别,但我还会将其他内容写入文件,这些内容不是与打包值位于同一行的打包值,这也是使用 StreamWriter 的原因.

I don't know if this makes a difference, but I will also be writing other things to the file that aren't the packed values on the same lines as the packed values, and thus the reason for using the StreamWriter.

推荐答案

这是一个编码问题.如果你写 *byte*s,它不应该发生.

It's an encoding issue. It shouldn't happen if you write *byte*s.

BinaryWriter writer = new BinaryWriter(someStream);
write.Write((byte)123); // just an example! not a "that's how you should do it"

更好的解决方案是选择正确的编码.但是你的角色在文件中的外观真的很重要吗?

A better solution would be to select the proper encoding. But does the way your characters look in the file really matter?

这篇关于使用 StreamWriter 的 C# 脚本会创建额外的字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆