编辑代码

#include <iostream>
using namespace std;
int main() {
    private string EpcEncoding(string sku, string sn)
{
    if (string.IsNullOrEmpty(sku))
    {
        cc.MsgBox("请填写SKU");
        return string.Empty;
    }

    if (sku.Length != 13)
    {
        cc.MsgBox("SKU长度错误,应该为13位");
        return string.Empty;
    }

    // 检查第一个字符是否为数字
    if (!char.IsDigit(sku[0]))
    {
        cc.MsgBox("SKU的第一个字符必须为数字");
        return string.Empty;
    }

    if (string.IsNullOrEmpty(sn) || sn.Length != 5)
    {
        cc.MsgBox("SN错误");
        return string.Empty;
    }

    char yearCode = sku[1];
    if (yearCode < 'A' || yearCode > 'Z')
    {
        cc.MsgBox("SKU年份码错误");
        return string.Empty;
    }
    int year = yearCode - 'M' + 23;
    string sku2 = sku.Substring(0, 1) + year.ToString() + sku.Substring(2);

    // 验证sku2是否为有效数字
    if (!long.TryParse(sku2, out long skuLong))
    {
        cc.MsgBox("SKU转换失败,包含无效字符");
        return string.Empty;
    }

    BigInteger skuI = (BigInteger)skuLong << 34;
    BigInteger header = (BigInteger)0x3138 << 80;
    if (!long.TryParse(sn, out long snLong))
    {
        cc.MsgBox("SN必须为数字");
        return string.Empty;
    }
    BigInteger snI = (BigInteger)snLong;

    BigInteger value = header + skuI + snI;

    // 处理字节数组,确保长度为12字节
    byte[] epc = value.ToByteArray();
    List<byte> epcList = new List<byte>(epc);

    // 移除可能存在的符号填充字节(正数时可能添加的0x00)
    if (epcList.Count > 0 && epcList.Last() == 0)
        epcList.RemoveAt(epcList.Count - 1);

    int desiredLength = 12;
    if (epcList.Count > desiredLength)
    {
        // 取最后12字节(小端序,高位在后)
        epcList = epcList.GetRange(epcList.Count - desiredLength, desiredLength);
    }
    else if (epcList.Count < desiredLength)
    {
        // 前面补零(小端序,低位在前,补零不影响高位)
        epcList.InsertRange(0, Enumerable.Repeat((byte)0, desiredLength - epcList.Count));
    }

    // 反转字节顺序为大端序
    byte[] epcReverse = new byte[desiredLength];
    for (int i = 0; i < desiredLength; i++)
        epcReverse[i] = epcList[desiredLength - 1 - i];

    return cc.BinToStringSimple(epcReverse);
}
    cout << "Hello world!    - cpp.jsrun.net." << endl;
	return 0;
}