DataType for storing a long serial number (10 bytes)

We have a device with a 10-byte serial number that should be read in our application and saved as .net. On the device, it is stored as an unsigned 10-byte (80-bit) number. I do not expect that we will perform any mathematical operations on this number, but only show it to the user.

The .NET framework does not have a built-in UNIT128 for storing this data type. My suggestion is to save this data type - create an array of 10 elements and read the data in this array. Are there any better solutions to this problem?

Note. I saw in this question that the GUID is a 128-byte signed integer, but it seems like a bad idea to use the GUID in this way. Any other suggestions?

0
source share
5 answers

You should use an array of bytes.

+4
source

If you only show it, why not use a string? If you want additional protection against accidental changes, you can even transfer this to any business object.

+1
source

I agree with @SLaks, you should use an array of bytes. However, BigInteger, http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx , can also work because it theoretically has no lower / upper bounds. This is only for .NET 4. A byte array would still be a better choice.

+1
source

If you are not calculating by number, what happened to System.String?

0
source

You have few options. Any of them depend on how you use the data. You can pack it in Guid, use an array of bytes, use your own Struct, or pack it as Base64 or HEX and paste it into a string.

[StructLayout( LayoutKind.Explicit)] public struct MyStruct { public MyStruct(byte[] buffer) { if (buffer.Length != 10) throw new ArgumentOutOfRangeException(); High = BitConverter.ToUInt16(buffer, 0); Low = BitConverter.ToUInt64(buffer, 2); } [FieldOffset(0)] public ushort High; //2 bytes [FieldOffset(2)] public ulong Low; //8 bytes public byte[] Bytes { get { return BitConverter.GetBytes(High) .Concat(BitConverter.GetBytes(Low)) .ToArray(); } } public override string ToString() { return Convert.ToBase64String(Bytes); } public static MyStruct Parse(string toParse) { var bytes = Convert.FromBase64String(toParse); return new MyStruct(bytes); } } 
0
source

Source: https://habr.com/ru/post/896485/


All Articles