简体   繁体   中英

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. I've read them into char dataLength[3] , but I don't know what to do next. 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:

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?

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. For example, int('3' - '0') = 3. In C++, you can also use string stream this way:

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. you can use while loop;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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