简体   繁体   English

如何在C / C ++中将char [3]从FLV结构转换为int

[英]How to convert the char[3] from an FLV struct to an int in c/c++

I have to read tags from an FLV file and there are three chars that identify the data length. 我必须从FLV文件中读取标签,并且有三个用于标识数据长度的字符。 I've read them into char dataLength[3] , but I don't know what to do next. 我已经将它们读入char dataLength[3] ,但是我不知道下一步该怎么做。 It's binary, not ASCII, so it's not as simple as using atoi() to convert char xxx[] = "123" into the integer 123. Here is my C struct for the tags: 它是二进制而不是ASCII,所以它不像使用atoi()char xxx[] = "123"转换为整数123那样简单。这是标签的C结构:

typedef struct {
  int previousLength;

  char identify; // 8: AUDIO, 9:VIDEO 18: SCRIPT
  char dataLength[3];

  char time[3];
  char timeExt;

  char streamID[3]; // always 0;
  char unused__;
} FLVTag;

I can read the tags from the file, but how do I convert dataLength into an int? 我可以从文件中读取标签,但是如何将dataLength转换为int?

int length = ((unsigned int)xxx[0]) << 16 + ((unsigned int)xxx[1]) << 8 + ((unsigned int)xxx[2]);

根据http://osflash.org/flv的 Big-endian。

You can use int(char - '0') to convert char to corresponding decimal integer. 您可以使用int(char - '0')将char转换为相应的十进制整数。 For example, int('3' - '0') = 3. In C++, you can also use string stream this way: 例如, int('3' - '0') =3。在C ++中,您还可以通过以下方式使用字符串流:

stringstream ss;
ss << char;
int n;
ss >> n;

You can do this: 你可以这样做:

int a = (dataLength[0]-'0')*100+(dataLength[1]-'0')*10+(dataLength[2]-'0');

if this char array have more item. 如果此char数组具有更多项。 you can use while loop; 你可以使用while循环;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM