コード例 #1
0
ファイル: Sgtin96.cs プロジェクト: lluvr/sgtin96
        /// <summary>
        /// Decode hex string into Sgtin class instance <see cref="Sgtin96"/>
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static Sgtin96 HexStringToSgtin96(string hex)
        {
            // validate by string length and convert to bit array
            if (hex.Length != 24)
            {
                throw new FormatException("Provided hexString must be 24 characters long.");
            }
            BitArray bits = SgtinDecoder.HexStringToBitArray(hex);

            // validate and get header
            uint header = SgtinDecoder.BitArrayToUInt(bits, 0, 8);

            if (header != Sgtin96Header)
            {
                throw new FormatException("Provided header is not valid SGTIN96 header.");
            }

            // get filter and partition uInt from bits
            SgtinFilter filter    = (SgtinFilter)SgtinDecoder.BitArrayToUInt(bits, 8, 3);
            byte        partition = (byte)SgtinDecoder.BitArrayToUInt(bits, 11, 3);

            // decode partition related values
            ulong companyPrefix;
            uint  itemReference;

            SgtinDecoder.DecodePartition(bits, SgtinPartitions, partition, out companyPrefix, out itemReference);

            // get serial uLong
            ulong serialReference = SgtinDecoder.BitArrayToULong(bits, 58, 38);

            // init and return new class instance
            return(new Sgtin96(Sgtin96Header, filter, partition, companyPrefix, itemReference, serialReference));
        }
コード例 #2
0
        /// <summary>
        /// Decode partition related sgtin values using default partition table
        /// </summary>
        /// <param name="bits"></param>
        /// <param name="partitionDefaults"></param>
        /// <param name="partition"></param>
        /// <param name="companyPrefix"></param>
        /// <param name="itemReference"></param>
        internal static void DecodePartition(BitArray bits, ISgtinPartition[] partitionDefaults, byte partition, out ulong companyPrefix, out uint itemReference)
        {
            if (partitionDefaults.Length < partition || partitionDefaults[partition] == null)
            {
                throw new FormatException("Partition value is invalid or undefined.");
            }

            int             startingBit      = 14;
            ISgtinPartition partitionDefault = partitionDefaults[partition];

            int companyPrefixDefaultBits = partitionDefault.CompanyPrefixBits;
            int itemReferenceDefaultBits = partitionDefault.ItemReferenceBits;

            companyPrefix = SgtinDecoder.BitArrayToULong(bits, startingBit, companyPrefixDefaultBits);
            itemReference = SgtinDecoder.BitArrayToUInt(bits, startingBit + companyPrefixDefaultBits, itemReferenceDefaultBits);
        }