如何在这个片段中避免reinterpret_cast? [英] how to avoid reinterpret_cast in this snippet?

查看:89
本文介绍了如何在这个片段中避免reinterpret_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个无符号字符缓冲区''缓冲区[1024]''我需要将

的前12个字节转换为字符串。下面是一个应该有效的代码,

然而,我怎样才能避免reinterpret_cast运算符?

或者有一种简单的方法可以解决这个问题吗?

谢谢。

-KK

/ *尚未测试* /

typedef unsigned char BYTE

std :: string GetStringFromByteBuffer(const BYTE * const buffer,int pos)

{

const char * chAry = reinterpret_cast< const char *> (缓冲区+ pos);

std :: string tmp(chAry,12);

返回chAry;

}

Hello all,
I have a unsigned char buffer ''buffer[1024]'' and I need to convert the
first 12 bytes of it into a string. Below is a code that should work,
however, how can I avoid reinterpret_cast operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromByteBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cast <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}

推荐答案

KK写道:
大家好,
我有一个无符号字符缓冲区''缓冲区[1024] ''我需要将它的前12个字节转换为字符串。下面是一个应该有效的代码,但是,我怎样才能避免使用reinterpret_cast操作符?
或者有一种简单的方法来解决这个问题吗?
谢谢。
-KK
/ *尚未测试* /
typedef unsigned char BYTE
std :: string GetStringFromByteBuffer(const BYTE * const buffer,int pos)
{
const char * chAry = reinterpret_cast< const char *> (缓冲区+ pos);
std :: string tmp(chAry,12);
返回chAry;
}
Hello all,
I have a unsigned char buffer ''buffer[1024]'' and I need to convert the
first 12 bytes of it into a string. Below is a code that should work,
however, how can I avoid reinterpret_cast operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromByteBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cast <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}



你不能。 unsigned char *和char *不可转换。即使在

系统中,char本身就是无符号的,它也是一种独特的类型。然而

演员应该是安全的。


You can''t. unsigned char* and char* are not convertible. Even on
systems where char is inherently unsigned it''s a distinct type. The
cast however should be safe.


* KK:
大家好,我有一个unsigned char缓冲区''缓冲区[1024]''我需要将它的前12个字节转换为字符串。下面是一个应该有效的代码,但是,我怎样才能避免使用reinterpret_cast操作符?
或者有一种简单的方法来解决这个问题吗?
谢谢。
-KK
/ *尚未测试* /
typedef unsigned char BYTE
std :: string GetStringFromByteBuffer(const BYTE * const buffer,int pos)
{
const char * chAry = reinterpret_cast< const char *> (缓冲区+ pos);
std :: string tmp(chAry,12);
返回chAry;
}
Hello all,
I have a unsigned char buffer ''buffer[1024]'' and I need to convert the
first 12 bytes of it into a string. Below is a code that should work,
however, how can I avoid reinterpret_cast operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromByteBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cast <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}




如何关于


std :: string string12From(const BYTE buffer [],int pos)

{

return std :: string (缓冲区+ pos,缓冲区+ pos + 12);

}


顺便说一下,埋没像12这样的魔法数字不是一个好主意在代码中。


-

答:因为它弄乱了人们通常阅读文本的顺序。

Q :为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?



How about

std::string string12From( const BYTE buffer[], int pos )
{
return std::string( buffer+pos, buffer+pos+12 );
}

Btw., it''s not a good idea to bury magic numbers like 12 in the code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


KK写道:
大家好,
我有一个无符号字符缓冲区''缓冲区[1024]''我需要转换
将它的前12个字节转换成字符串。下面是一个应该有效的代码,但是,我怎样才能避免使用reinterpret_cast操作符?
或者有一种简单的方法来解决这个问题吗?
谢谢。
-KK
/ *尚未测试* /
typedef unsigned char BYTE
std :: string GetStringFromByteBuffer(const BYTE * const buffer,int pos)
{
const char * chAry = reinterpret_cast< const char *> (缓冲区+ pos);
std :: string tmp(chAry,12);
返回chAry;
}
Hello all,
I have a unsigned char buffer ''buffer[1024]'' and I need to convert the
first 12 bytes of it into a string. Below is a code that should work,
however, how can I avoid reinterpret_cast operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromByteBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cast <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}



除了Alf'的建议,这是另一个选择:


void GetStringFromByteBuffer(const BYTE * const buffer,

std :: string& s)

{

std :: copy(buffer,buffer + 12,s.begin());

}


BYTE buff [19];

int pos = 6;

std :: string str;


GetStringFromByteBuffer(

buff + pos,

str);


Ben


In addition to Alf''s suggestion, here is another choice:

void GetStringFromByteBuffer(const BYTE* const buffer,
std::string& s)
{
std::copy(buffer, buffer+12, s.begin());
}

BYTE buff[19];
int pos = 6;
std::string str;

GetStringFromByteBuffer(
buff + pos,
str);

Ben


这篇关于如何在这个片段中避免reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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