3

我的计费提供商需要以十六进制而不是 ASCII 格式获取消息,例如,我发送了一条 800 消息,流是:

42 00 30 38 30 30 a2 38 00 00 00 80 80 00 04 00

00 00 00 00 00 00 39 30 30 30 30 30 30 34 30 32

31 34 33 31 31 38 31 37 33 31 31 38 31 37 33 31

31 38 30 34 30 32 31 32 33 34 35 36 37 38 39 39

38 30 30 31

我可以使用该项目将消息创建为十六进制而不是 ASCII 吗?我是否只需要在发送之前转换消息(并在返回消息时转换回来)?

我会感谢你在这件事上的帮助

4

2 回答 2

2

您可以更改字段、位图和消息类型的格式化程序。

在 Template 类中查看项目的源代码。您需要创建自己的扩展 Iso8583 的类,并创建自己的具有 ASCII 位图和消息类型格式化程序的模板。

从 0.5.1 版本开始,您可以执行以下操作

public class AsciiIso : Iso8583
{
    private static readonly Template template;

    static AsciiIso()
    {
        template = GetDefaultIso8583Template();
        template.BitmapFormatter = Formatters.Ascii;
        template.MsgTypeFormatter = Formatters.Ascii;
    }

    public AsciiIso() : base(template)
    {
    }
}
于 2012-04-03T14:36:09.847 回答
0

“HEX 而不是 ASCII”是什么意思?HEX 字符串通常是仅由 [0-9A-F] 字符组成的 ASCII 编码字符串。

这是一个 C# 扩展方法,它将字节数组转换为十六进制数字对的字符串,表示原始字节数组的字节:

using System;
using System.Linq;
using System.Text;

namespace Substitute.With.Your.Project.Namespace.Extensions
{
    static public class ByteUtil
    {
        /// <summary>
        /// byte[] to hex string
        /// </summary>
        /// <param name="self"></param>
        /// <returns>a string of hex digit pairs representing this byte[]</returns>
        static public string ToHexString(this byte[] self)
        {
            return self
                .AsEnumerable()
                .Aggregate(
                    new StringBuilder(),
                    (sb, value)
                        => sb.Append(
                            string.Format("{0:X2}", value)
                           )
                ).ToString();
        }
    }
}

然后在其他地方你只是use扩展

using Substitute.With.Your.Project.Namespace.Extensions;

并这样称呼它

aByteArray.ToHexString();

(代码未经测试;YMMW)

于 2012-04-03T07:34:57.923 回答