Convert message from ASCII to HEX ISO8583.net

2.8k Views Asked by At

my billing provider needs to get the message in HEX and not in ASCII, for example, I've sent a 800 message and the stream was:

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

Can i use the project to create the message as HEX instead of ASCII? Do i just need to convert the message before i send it (and convert back on return message)?

I'll appreciate your help in the matter

2

There are 2 best solutions below

0
On

What do you mean by "HEX and not ASCII"? A HEX string is usually an ASCII encoded string consisting of [0-9A-F] characters only.

Here is a C# extension method that converts byte arrays into strings of hex-digit pairs representing the bytes of the original byte array:

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();
        }
    }
}

Then elsewhere you just use the extension

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

and call it like this

aByteArray.ToHexString();

(the code is untested; YMMW)

1
On

You can change the formatters for the fields, bitmap and message types.

Look at the source of the project at the Template class. You need to create your own class that extends Iso8583 and create your own template that has ASCII bitmap and message type formatters.

From the 0.5.1 release you can do the following

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)
    {
    }
}