Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从以下字节中提取日期时间?_C#_.net - Fatal编程技术网

C# 如何从以下字节中提取日期时间?

C# 如何从以下字节中提取日期时间?,c#,.net,C#,.net,我有以下5个字节的附加图像,需要从中提取日期时间。我知道我需要进行位移位,可能需要使用按位and,但无法从字节中获得正确的信息 我最近回答了一个关于C#中的位移位和整数打包的问题 可能是助手类写的,在那种情况下可以作为有用的起点给你 public static class BinaryConverter { public static BitArray ToBinary(this int numeral) { return new BitArray(new[] {

我有以下5个字节的附加图像,需要从中提取日期时间。我知道我需要进行位移位,可能需要使用按位and,但无法从字节中获得正确的信息


我最近回答了一个关于C#中的位移位和整数打包的问题

可能是助手类写的,在那种情况下可以作为有用的起点给你

public static class BinaryConverter
{
    public static BitArray ToBinary(this int numeral)
    {
        return new BitArray(new[] { numeral });
    }

    public static int ToNumeral(this BitArray binary)
    {
        if (binary == null)
            throw new ArgumentNullException("binary");
        if (binary.Length > 32)
            throw new ArgumentException("must be at most 32 bits long");

        var result = new int[1];
        binary.CopyTo(result, 0);
        return result[0];
    }

    public static BitArray Take (this BitArray current, int length )
    {
        if (current.Length < length)
            throw new Exception("Invalid length parameter");

        List<bool> taken = new List<bool>();

        for (int i = 0; i < length; i++)
                taken.Add(current.Get(i));

        return new BitArray(taken.ToArray());
    }

    public static BitArray Shift (this BitArray current, int length )
    {
        if (current.Length < length)
            throw new Exception("Invalid length parameter");

        List<bool> shifted = new List<bool>();

        for (int i = 0; i < current.Length - length; i++)
            shifted.Add(current.Get(length + i));

        return new BitArray(shifted.ToArray());
    }

    public static BitArray FitSize (this BitArray current, int size)
    {
        List<bool> bools = new List<bool>() ;
        bools = bools.InitBoolArray(size);

        for (int i = 0; i < current.Count; i++)
                bools[i] = current.Get(i) ;

        return new BitArray(bools.ToArray());
    }

    public static List<bool> InitBoolArray(this List<bool> current, int size)
    {
        List<bool> bools = new List<bool> ();

        for (int i = 0; i < size; i++)
            bools.Add(false);

        return bools ;
    }
公共静态类二进制转换器
{
公共静态位数组ToBinary(此整数数字)
{
返回新的位数组(新[]{numeric});
}
公共静态int-ToNumeral(此位数组二进制)
{
if(二进制==null)
抛出新的ArgumentNullException(“二进制”);
如果(binary.Length>32)
抛出新ArgumentException(“长度必须最多为32位”);
var结果=新整数[1];
二进制.CopyTo(结果为0);
返回结果[0];
}
公共静态位数组获取(此位数组当前,整数长度)
{
如果(当前长度<长度)
抛出新异常(“长度参数无效”);
获取的列表=新列表();
for(int i=0;i
这里是对这个答案的引用

在上面的链接中显示了如何在同一个整数上打包小数字,您的五个字节与该问题非常接近

可能是这样吗

int yearBase = 1993;

int year = yearBase + (int) ((bytes[4] & 0xF0) >> 4) | ((bytes[3] & 0xE0) >> 1);
int month = (int) (bytes[4] & 0x0F);
int day = (int) (bytes[3] & 0x1F);
int hour = (int) ((bytes[2] & 0xF8) >> 3);
int min = (int) (((bytes[2] & 0x03) << 3) | ((bytes[1] & 0xE0) >> 5));
int sec = (int) ((bytes[1] & 0x1F) << 1) | ((bytes[0] & 0x80) >> 7);
int hundreths = (int) (bytes[0] & 0x7F);
int年基=1993年;
int year=yearBase+(int)((字节[4]&0xF0)>>4)|((字节[3]&0xE0)>>1);
整数月份=(整数)(字节[4]&0x0F);
整数天=(整数)(字节[3]&0x1F);
整数小时=(整数)((字节[2]&0xF8)>>3);
int min=(int)((字节[2]&0x03)>5));
int sec=(int)((字节[1]&0x1F)>7);
整数百分之=(整数)(字节[0]&0x7F);

您目前使用的代码是什么?您能详细解释一下这两个
Y
是如何计算的吗?它们是一个单独的值,位是相关的,还是每一个都代表一年中最后两个数字的一个数字?它们彼此分开的事实让我觉得每个都是一个数字,但您只能代表0-7它在
字节3
中使用的3位。如果将它们串联起来,则得到0-127,因此可以将其用于0-99,检查1字节=(Y 2000年的基本偏移量是多少?Matt这给了我一些好的日期,我必须根据我知道的日期来测试。我必须为此追踪一些数据,但上面肯定是正确的,谢谢。