一个static_cast引发的char转int问题

写了一段程序,放在某个群里,一同学说我这里用staic_cast有问题,不安全!

还请大家指点指点!


// 读取出来大于'A'--17小于'a'的要重新计算值
inline int CalcHigh_a( const char strHex )
{
return static_cast<int>( strHex ) - CALCLETTER;
}
// 读取出来大于9小于'A'的要重新计算值
inline int CalcHigh_A( const char strHex )
{
return static_cast<int>( strHex ) - CALCHIGH;
}



附上完整的代码:-------------我是觉得写得有点不太好看,需要怎么调整一下?


#include "stdafx.h"
#include "iostream"
using namespace std;

#define TRANSPOSEDIGIT 4
// assist function
#define DECIMALLENGTH0 1
#define DECIMALLENGTH1 2
#define DECIMALLENGTH2 3
#define DECIMALLENGTH3 4

#define CONVERT 48
#define CALCHIGH ( 48 + 7 )
#define CALCLETTER ( 48 + 7 + 32 )
#define LIMITNINE 16
#define LIMITA 31

// 读取出来大于'A'--17小于'a'的要重新计算值
inline int CalcHigh_a( const char strHex )
{
return static_cast<int>( strHex ) - CALCLETTER;
}
// 读取出来大于9小于'A'的要重新计算值
inline int CalcHigh_A( const char strHex )
{
return static_cast<int>( strHex ) - CALCHIGH;
}

// 计算十六进制字符串的值
int CalcStrDecimal( const char* strHex, const int nHexLength )
{
int iResult = 0;
int ii = 0;
if ( DECIMALLENGTH0 == nHexLength )
{
ii = static_cast<int>( strHex[ 0 ] ) - CONVERT;
if ( ii > LIMITNINE && ii < LIMITA )
{
ii = CalcHigh_A( strHex[ 0 ] );
}
else if ( ii > LIMITA )
{
ii = CalcHigh_a( strHex[ 0 ] );
}
iResult += ii;
}
else if ( DECIMALLENGTH1 == nHexLength )
{
for ( int i = 0; i < nHexLength; i++ )
{
ii = static_cast<int>( strHex[ i ] ) - CONVERT;
if ( ii > LIMITNINE && ii < LIMITA )
{
ii = CalcHigh_A( strHex[ 0 ] );
}
else if ( ii > LIMITA )
{
ii = CalcHigh_a( strHex[ 0 ] );
}
switch( i )
{
case 0:
ii = ii << TRANSPOSEDIGIT;
break;
default:
break;
}
iResult += ii;
}
}
else if ( DECIMALLENGTH2 == nHexLength )
{
for ( int i = 0; i < nHexLength; i++ )
{
ii = static_cast<int>( strHex[ i ] ) - CONVERT;
if ( ii > LIMITNINE && ii < LIMITA )
{
ii = CalcHigh_A( strHex[ 0 ] );
}
else if ( ii > LIMITA )
{
ii = CalcHigh_a( strHex[ 0 ] );
}
switch( i )
{
case 0:
ii = ii << TRANSPOSEDIGIT << TRANSPOSEDIGIT;
break;
case 1:
ii = ii << TRANSPOSEDIGIT;
break;
default:
break;
}
iResult += ii;
}
}
else if ( DECIMALLENGTH3 == nHexLength )
{
for ( int i = 0; i < nHexLength; i++ )
{
ii = static_cast<int>( strHex[ i ] ) - CONVERT;
if ( ii > LIMITNINE && ii < LIMITA )
{
ii = CalcHigh_A( strHex[ 0 ] );
}
else if ( ii > LIMITA )
{
ii = CalcHigh_a( strHex[ 0 ] );
}
switch( i )
{
case 0:
ii = ii << TRANSPOSEDIGIT << TRANSPOSEDIGIT<< TRANSPOSEDIGIT;
break;
case 1:
ii = ii << TRANSPOSEDIGIT << TRANSPOSEDIGIT;
break;
case 2:
ii = ii << TRANSPOSEDIGIT;
break;
default:
break;
}
iResult += ii;
}
}
return iResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
// length == 3
// char strTest[ 5 ] = "A202";
// char strTest[ 5 ] = "a202";
// char strTest[ 5 ] = "B202";
// char strTest[ 5 ] = "b202";
// char strTest[ 5 ] = "c202";
// char strTest[ 5 ] = "C202";
// char strTest[ 5 ] = "d202";
// char strTest[ 5 ] = "D242";
// char strTest[ 5 ] = "0202";
// int lValue = CalcStrDecimal( strTest, 4 );

// char strTest[ 5 ] = "002";
char strTest[ 5 ] = "A22";
// char strTest[ 5 ] = "AAA";
int lValue = CalcStrDecimal( strTest, 3 );

// char strTest[ 5 ] = "02";
// char strTest[ 5 ] = "a2";
// char strTest[ 5 ] = "AA";
// int lValue = CalcStrDecimal( strTest, 2 );

// char strTest[ 5 ] = "A";
// char strTest[ 5 ] = "a";
// int lValue = CalcStrDecimal( strTest, 1 );

cout << lValue << endl;

return 0;
}

// hex202 == int514

...全文
410 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
强制类型转换总是不安全,要说明这是很难的。
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]
参考:
区别:static _cast、dynamic _cast、reinterpret_cast和const_cast
[/Quote]

这也没有提到char转int的安全问题,仅仅说到下转型的。恩 看完吧
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
看不出来什么问题
[/Quote]

我也没发现问题,可是当我知道我在重做strtol的事我就极度崩溃了。
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
引用 5 楼 的回复:

引用 2 楼 的回复:
没看出来static_cast成int有啥问题。
char表示的整数值范围是int的真子集。
倒是这里干嘛这么麻烦,还依赖基本字符集中某些元素的连续性……直接用<cchar>里的一些函数不行么?


这个怎么说?cchar>里的一些函数?

口胡了,顺手cwchar然后改掉,不知道怎么回事又抽到Ctrl+Z orz...
#i……
[/Quote]

嗯,我懂了,我有点崩溃了,居然在重新造轮子。。我想对着天空说,fuck!
W170532934 2012-07-12
  • 打赏
  • 举报
回复
看不出来什么问题
FrankHB1989 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 2 楼 的回复:
没看出来static_cast成int有啥问题。
char表示的整数值范围是int的真子集。
倒是这里干嘛这么麻烦,还依赖基本字符集中某些元素的连续性……直接用<cchar>里的一些函数不行么?


这个怎么说?cchar>里的一些函数?
[/Quote]
口胡了,顺手cwchar然后改掉,不知道怎么回事又抽到Ctrl+Z orz...
#include<cctype>
using std::isxdigit; //...
FrankHB1989 2012-07-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 4 楼 的回复:
strtol


现在用的好像不是string。
[/Quote]
std::string?那就是std::stol了,和strtol两回事。
未日机甲 2012-07-12
  • 打赏
  • 举报
回复
你自己很享受造轮子的过程,加油吧
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
strtol
[/Quote]

我以为用atoi都已经很奢侈了,更何况strtol。。你说是吧
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
strtol
[/Quote]

现在用的好像不是string。
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
没看出来static_cast成int有啥问题。
char表示的整数值范围是int的真子集。
倒是这里干嘛这么麻烦,还依赖基本字符集中某些元素的连续性……直接用<cchar>里的一些函数不行么?
[/Quote]

这个怎么说?cchar>里的一些函数?
赵4老师 2012-07-12
  • 打赏
  • 举报
回复
strtol
未日机甲 2012-07-12
  • 打赏
  • 举报
回复
完全没有问题,直接赋值都行的。int完全能兼容char
FrankHB1989 2012-07-12
  • 打赏
  • 举报
回复
没看出来static_cast成int有啥问题。
char表示的整数值范围是int的真子集。
倒是这里干嘛这么麻烦,还依赖基本字符集中某些元素的连续性……直接用<cchar>里的一些函数不行么?
  • 打赏
  • 举报
回复
呃,没看出来有啥问题

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧