如何转换vector< unsigned char>诠释? [英] How to convert vector<unsigned char> to int?

查看:67
本文介绍了如何转换vector< unsigned char>诠释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 vector< unsigned char> 归档为二进制数据.我需要从vector(2个字节)中提取2个项目,并将其转换为整数.不是C风格怎么办呢?

I have vector<unsigned char> filed with binary data. I need to take, lets say, 2 items from vector(2 bytes) and convert it to integer. How this could be done not in C style?

推荐答案

请使用移位运算符/按位运算.

Please use the shift operator / bit-wise operations.

int t = (v[0] << 8) | v[1];

此处提出的所有基于强制转换/联合的解决方案都是AFAIK未定义的行为,并且可能会在利用

All the solutions proposed here that are based on casting/unions are AFAIK undefined behavior, and may fail on compilers that take advantage of strict aliasing (e.g. GCC).

这篇关于如何转换vector&lt; unsigned char&gt;诠释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆