Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
M

Member_14858258

@Member_14858258
About
Posts
2
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to unpack packed decimal
    M Member_14858258

    See my answer off initial post.

    C# tutorial

  • How to unpack packed decimal
    M Member_14858258

    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

    C# tutorial
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups