将消息从ASCII转换为HEX ISO8583.net

时间:2012-04-02 16:46:50

标签: iso8583 openiso8583.net

我的结算提供商需要以HEX格式而不是ASCII格式获取邮件,例如,我发送了800条消息,然后信息流为:

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

<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

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

我将非常感谢你对此事的帮助

2 个答案:

答案 0 :(得分: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)
    {
    }
}

答案 1 :(得分:0)

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

这是一个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)

相关问题