问题列表 - 第318073页

如何将大字节对象转换为 32 位整数值列表?

我有一个bytes对象,其长度(以字节为单位)是 4 的倍数。我想将其转换为32 位整数值n的列表。n/4

什么是有效的方法来做到这一点?

例如,转换x为y:

x = b'\x01\x02\x03\x04\xef\xbe\xad\xde'
y = [0x04030201, 0xdeadbeef]
Run Code Online (Sandbox Code Playgroud)

更新1:找出这段代码。有没有更简单的方法(一种内衬解决方案)?

x = b'\x01\x02\x03\x04\xef\xbe\xad\xde'
ls = [x[n:(n+4)] for n in range(0, len(x), 4)]
y = []
for xx in [int.from_bytes(xx, 'little') for xx in ls]:
    y.append(xx)
Run Code Online (Sandbox Code Playgroud)

更新 2:我意识到我可以合并两个循环:

x = b'\x01\x02\x03\x04\xef\xbe\xad\xde'
y = [int.from_bytes(x[n:(n+4)], 'little') for n in range(0, len(x), 4)]
Run Code Online (Sandbox Code Playgroud)

这是否像它所得到的那么优雅?

python type-conversion python-3.x

2
推荐指数
2
解决办法
106
查看次数

如何平滑CSS中的线性平移动画

所以我想尝试使用纯CSS制作一艘宇宙飞船并尝试添加一些细节。我在宇宙飞船的主体上添加了 4 个点,我想给出旋转的效果。由于它是 2D 模型,我们只能从前面看到旋转,就像选框标签一样。我添加了关键帧,但它并不流畅。当动画时间停止时,它会突然切断动画。我怎样才能解决这个问题,让它看起来像不停地旋转。这是代码:

<div class="spaceship">
  <div class="glass">
    <div class="bottom">
      <div class="point1"></div>
      <div class="point2"></div>
      <div class="point3"></div>
      <div class="point4"></div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

和CSS:

<div class="spaceship">
  <div class="glass">
    <div class="bottom">
      <div class="point1"></div>
      <div class="point2"></div>
      <div class="point3"></div>
      <div class="point4"></div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
   

* {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      background: #333;
    }

    .spaceship {
      position: absolute;
      height: 200px;
      aspect-ratio: 1;
    }

    .spaceship .glass {
      height: 100px;
      aspect-ratio: 1;
      /*   background: red; */
      background: rgba(255, 255, 255, 0.19);
      border-radius: …
Run Code Online (Sandbox Code Playgroud)

css

2
推荐指数
1
解决办法
43
查看次数

为什么 (int*)++ 前进 4 而 (int**)++/(int***)++ 前进 8?所有指针都应该是 4 字节还是 8 字节?

请尝试以下代码:

void intArrayAsPointerPrinter(int iSize){

    int * p = 0;
    int ** q = 0;
    int *** r = 0;
    for(int i = 0; i < iSize; i++) {
        printf("%p %p %p\n", p,q,r);
        p++;
        q++;
        r++;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用大小 3 调用它,我得到以下输出:

0x0 0x0 0x0
0x4 0x8 0x8
0x8 0x10 0x10
Run Code Online (Sandbox Code Playgroud)

问:为什么(int*)++前进4而其他前进8?所有指针都应该是 4 字节还是 8 字节?

c pointers

1
推荐指数
1
解决办法
69
查看次数

Python 方法重写 - 派生类中的参数比基类中的参数更具体

假设我想创建一个名为 的抽象基类Document。我希望类型检查器保证其所有子类都实现一个名为 的类方法from_paragraphs,该方法从一系列Paragraph对象构造一个文档。然而, aLegalDocument只能从LegalParagraph对象构造,而AcademicDocument- 只能从AcademicParagraph对象构造。

我的直觉是这样做:

from abc import ABC, abstractmethod
from typing import Sequence


class Document(ABC):
    @classmethod
    @abstractmethod
    def from_paragraphs(cls, paragraphs: Sequence["Paragraph"]):
        pass


class LegalDocument(Document):
    @classmethod
    def from_paragraphs(cls, paragraphs: Sequence["LegalParagraph"]):
        return  # some logic here...


class AcademicDocument(Document):
    @classmethod
    def from_paragraphs(cls, paragraphs: Sequence["AcademicParagraph"]):
        return  # some logic here...


class Paragraph:
    text: str


class LegalParagraph(Paragraph):
    pass


class AcademicParagraph(Paragraph):
    pass
Run Code Online (Sandbox Code Playgroud)

然而,Pyright 对此有所抱怨,因为from_paragraphs在派生类上违反了里氏替换原则。如何确保每个派生类都实现某种from_paragraphs类型?Paragraph

python methods inheritance overriding python-3.x

5
推荐指数
1
解决办法
81
查看次数

从函数返回后字符串似乎被释放

我目前正在尝试创建一个简单的函数来读取文件的内容并将其存储到 astd::string或char数组中。我遇到了这样的问题:数据在函数内完全读取,但在调用函数时似乎被删除/释放。

\n

我已将这个问题隔离到一个小程序中:

\n

入口.cpp

\n
#include <string>\n#include <fstream>\n#include <iostream>\n\nconst char* ReadFile(const std::string& path) {\n\n    std::ifstream file;\n\n    try {\n        file.exceptions(std::ifstream::failbit | std::ifstream::badbit);\n        file.open(path);\n\n        if (!file.is_open()) {\n            throw;\n        }\n    }\n    catch (std::exception e) { //file does not exist\n        std::string errmsg = "Unable to open file at path: " + path;\n\n        std::cout << errmsg << std::endl;\n\n        throw new std::exception(errmsg.c_str());\n    }\n\n\n    std::string contents;\n    try {\n        contents = std::string{ std::istreambuf_iterator<char>{file}, {} };\n    }\n    catch (const std::ifstream::failure& e) {\n\n        std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ fstream lifetime ifstream

3
推荐指数
1
解决办法
102
查看次数

awk 将非常大的数字从十六进制转换为十进制的输出不准确

我有以下数据:“210000000080401”。我需要将十六进制“210000000080401”转换为其十进制形式。

使用 Awk 没有给出正确的结果:

echo 210000000080401|awk -Wposix '{printf "%d %s\n", "0x" $1, $0}'
148618787703751680 210000000080401
Run Code Online (Sandbox Code Playgroud)

而正确的值为 148618787703751681

echo 210000000080401 | tr '[:lower:]' '[:upper:]'|xargs -i{} bash -c 'printf "%d\n" 0x{}'
148618787703751681
Run Code Online (Sandbox Code Playgroud)

为什么 awk 没有给出正确的输出?

shell awk

1
推荐指数
1
解决办法
67
查看次数

为什么在 C++ 概念示例中添加对 std::array 的支持会导致“调用时没有匹配的函数”编译错误?

我正在使用cppreference.com - Constraints and Concepts (Since C++20)中的示例学习 C++ 概念,我将在此处将其重现为:

template <typename T> 
requires std::integral<T> || std::floating_point<T>
constexpr double average(std::vector<T> const &vec) {
    const double sum = std::accumulate(vec.begin(), vec.end(), 0.0);        
    return sum / vec.size();
}

int main() 
{
    std::vector sVec { 1, 2, 3, 4, 5};
    assert(average(sVec) == 3); 
}
Run Code Online (Sandbox Code Playgroud)

我添加了 std::array 的情况:

template <typename T, int size>  // add size
requires std::integral<T> || std::floating_point<T>
constexpr double average(const std::array<T, size> &arr) {  // use std::array<T, size> &arr) instead
    const double …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts

-1
推荐指数
1
解决办法
132
查看次数

抽象特征中的“检查可见性时检测到循环”

我试图让工作成为一种依赖于多个层面的其他抽象特征的特征。我遇到了这个错误:

error[E0391]: cycle detected when checking effective visibilities
   |
note: ...which requires computing type of `wavefront::<impl at src/wavefront.rs:28:1: 47:46>::pos_iterator::{opaque#0}`...
  --> src/wavefront.rs:54:34
   |
54 |     fn pos_iterator(&'a self) -> impl Iterator<Item = Self::Vert3D>
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires computing type of opaque `wavefront::<impl at src/wavefront.rs:28:1: 47:46>::pos_iterator::{opaque#0}`...
  --> src/wavefront.rs:54:34
   |
54 |     fn pos_iterator(&'a self) -> impl Iterator<Item = Self::Vert3D>
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `wavefront::<impl at src/wavefront.rs:28:1: 47:46>::pos_iterator`...
  --> src/wavefront.rs:54:5
   |
54 |     fn pos_iterator(&'a self) -> impl Iterator<Item = …
Run Code Online (Sandbox Code Playgroud)

compiler-errors traits rust

5
推荐指数
1
解决办法
448
查看次数

为什么 CUDA 内核的开头有一个未使用的数据移动?

我正在尝试研究SASS从非常基本的 CUDA 内核生成的文件。这是内核:

__global__ void kernel(const float * x,
                       float * y,
                       const uint num_rows,
                       const uint num_cols) {
    const uint num_elems = num_rows * num_cols;
    const uint tid = blockDim.x * blockIdx.x + threadIdx.x;
    for (uint idx = tid; idx < num_elems; idx += blockDim.x * gridDim.x) {
        y[idx] = x[idx];
    }
}
Run Code Online (Sandbox Code Playgroud)

这是SASS文件。

1   00007f26 14f69f00         MOV R1, c[0x0][0x28]
2   00007f26 14f69f10         S2R R0, SR_CTAID.X
3   00007f26 14f69f20         ULDC.64 UR4, c[0x0][0x178]
4   00007f26 14f69f30 …
Run Code Online (Sandbox Code Playgroud)

cuda sass

5
推荐指数
1
解决办法
90
查看次数

这个 Delphi 语法有什么作用?“一些AnsiChar := ^H;”

我正在将一个非常旧的组件转换为 Delphi 12,并且偶然发现了我不记得以前见过的语法。代码有点像这样:

someBoolean := key in [^H, #32..#255];
Run Code Online (Sandbox Code Playgroud)

我知道它正在测试key指定的 ANSI 字符集是否匹配或在范围内,但到底是什么^H?H如果我更改为A, , BDelphi似乎很乐意编译该行C或 ,1。该行在 Delphi 12 和 Delphi 7 下编译。我似乎找不到任何有关此的信息。这似乎是一个非常古老的语法,可能来自 Pascal 时代。有人能解释一下吗?

我在 Delphi 7 中准备了这个测试代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15: AnsiChar;
begin
  c1 := ^A;
  c2 := ^B;
  c3 := ^C;
  c4 := ^D;
  c5 := ^E;
  c6 := ^F;
  c7 …
Run Code Online (Sandbox Code Playgroud)

delphi char control-characters

3
推荐指数
1
解决办法
164
查看次数