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
  1. Home
  2. General Programming
  3. C#
  4. How to unpack packed decimal

How to unpack packed decimal

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deep7
    wrote on last edited by
    #1

    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

    M M 2 Replies Last reply
    0
    • D deep7

      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

      M Offline
      M Offline
      Member_14569158
      wrote on last edited by
      #2

      Hi, Have you found a solution for this. I am running in to the same issue

      M 1 Reply Last reply
      0
      • D deep7

        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

        M Offline
        M Offline
        Member_14858258
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Member_14569158

          Hi, Have you found a solution for this. I am running in to the same issue

          M Offline
          M Offline
          Member_14858258
          wrote on last edited by
          #4

          See my answer off initial post.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

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