How to unpack packed decimal
-
Hi, I have a file which has EBCDIC data. I need to convert this into ASCII. But since this EBCDIC data also has 'packed decimal' i'm not able to convert this. Kindly let me know how to unpack this packed decimal, then later i can convert them to human readable ASCII format. Thanks! Deepa
-
Hi, I have a file which has EBCDIC data. I need to convert this into ASCII. But since this EBCDIC data also has 'packed decimal' i'm not able to convert this. Kindly let me know how to unpack this packed decimal, then later i can convert them to human readable ASCII format. Thanks! Deepa
Hi, Have you found a solution for this. I am running in to the same issue
-
Hi, I have a file which has EBCDIC data. I need to convert this into ASCII. But since this EBCDIC data also has 'packed decimal' i'm not able to convert this. Kindly let me know how to unpack this packed decimal, then later i can convert them to human readable ASCII format. Thanks! Deepa
1. Make sure you are getting the file transferred as binary (BIN) not text from the AS/400 (MF). Default would be text which will alter your values before you can read them. 2. Here ya go (C#): ```
using System.Linq;
namespace SomeNamespace
{
public static class SomeExtensionClass
{
/// /// computes the actual decimal value from an IBM "Packed Decimal" 9(x)v9 (COBOL) format
///
/// byte[]
/// byte; decimal places, default 2
/// decimal
public static decimal FromPackedDecimal(this byte[] value, byte precision = 2)
{
if (value.Length < 1)
{
throw new System.InvalidOperationException("Cannot unpack empty bytes.");
}
double power = System.Math.Pow(10, precision);
if (power > long.MaxValue)
{
throw new System.InvalidOperationException(
$"Precision too large for valid calculation: {precision}");
}
string hex = System.BitConverter.ToString(value).Replace("-", "");
var bytes = Enumerable.Range(0, hex.Length)
.Select(x => System.Convert.ToByte($"0{hex.Substring(x, 1)}", 16))
.ToList();
long place = 1;
decimal ret = 0;
for (int i = bytes.Count - 2; i > -1; i--)
{
ret += (bytes[i] * place);
place *= 10;
}
ret /= (long)power;
return (bytes.Last() & (1 << 7)) != 0 ? ret * -1 : ret;
}
/// /// computes the actual decimal value from an IBM "Packed Decimal" 9(x)v9 (COBOL) format
///
/// string; EBCDIC
/// byte; decimal places, default 2
/// decimal
public static decimal FromPackedDecimal(this string value, byte precision = 2)
{
return System.Text.Encoding.GetEncoding("IBM037")
.GetBytes(value).FromPackedDecimal(precision);
}
}
}``` Details on specification: Packed Decimal Format Cheers! Willow
-
Hi, Have you found a solution for this. I am running in to the same issue
See my answer off initial post.