我获得了以下代码来读取视频文件的FOURCC代码:
fourcc = (int)cap.get(cv::CAP_PROP_FOURCC);
string fourcc_str = fmt::format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
std::cout << "CAP_PROP_FOURCC: " << fourcc_str << std::endl;此代码输出%c%c,它应该输出HDYC。如果我将代码修改为
fourcc = (int)cap.get(cv::CAP_PROP_FOURCC);
string fourcc_str = fmt::format("{:x}{:x}{:x}{:x}", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
std::cout << "CAP_PROP_FOURCC: " << fourcc_str << std::endl;我得到的输出是:
CAP_PROP_FOURCC: 48445943我尝试将fmt类型更改为:x,但得到了一个异常。
fourcc = (int)cap.get(cv::CAP_PROP_FOURCC);
string fourcc_str = fmt::format("{:c}{:c}{:c}{:c}", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
std::cout << "CAP_PROP_FOURCC: " << fourcc_str << std::endl;这段代码按照我的预期工作,并输出了FOURCC代码'HDYC‘
fourcc = (int)cap.get(cv::CAP_PROP_FOURCC);
char c1 = fourcc & 255;
char c2 = (fourcc >> 8) & 255;
char c3 = (fourcc >> 16) & 255;
char c4 = (fourcc >> 24) & 255;
std::cout << "CAP_PROP_FOURCC: " << c1 << c2 << c3 << c4 << std::endl;
CAP_PROP_FOURCC: HDYC如何使用带有正确语法的fmt来获得FOUCC HDYC?
发布于 2021-06-15 22:47:10
c说明符适用于int和{fmt} 7+ (godbolt):
#include <fmt/core.h>
int main() {
int fourcc = ('C' << 24) | ('Y' << 16) | ('D' << 8) | 'H';
std::string fourcc_str = fmt::format(
"{:c}{:c}{:c}{:c}", fourcc & 255, (fourcc >> 8) & 255,
(fourcc >> 16) & 255, (fourcc >> 24) & 255);
fmt::print(fourcc_str);
}输出:
HDYChttps://stackoverflow.com/questions/67973451
复制相似问题