an error not easily discovered
来源:互联网 发布:知乎 马三娘你凭啥骑马 编辑:程序博客网 时间:2024/06/04 19:59
The following code is convert byte to hex.
When code is writed like 1, when text[i]'s high nibble byte
=F, it will cause htext[i]=0x00, i.e., null.
Therefore, it will produce wrong result.
The cause is that the compiler first does signed extend against text[i],
if text[i]'s high nibble byte=F, assume it 0xF5, the signed extend will let
it 0xFFFFFFF5, the shift right 4 bits, this will lead to 0XFFFFFFFF;
Therefore, hex[(text[i] >> 4)] will be hex[0xFFFFFFFF], it will produce 0,
or null, or segment fault. andhex[(text[i] & 0x0f)] = hex[0x0F] = 'F'
The correct code is write like 2.
1.
void hex_dump(char *text, char *htext, int len)
{int i = 0;
static char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for(i = 0; i < len; i++) {
htext[2 * i] = hex[(text[i] >> 4)];
htext[2 * i + 1] = hex[(text[i] & 0x0f)];
}
key[len * 2] = 0x00;
return;
}
2.
{
int i = 0;
static char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for(i = 0; i < len; i++) {
htext[2 * i] = hex[(text[i] >> 4)];
htext[2 * i + 1] = hex[(text[i] & 0x0f)];
}
key[len * 2] = 0x00;
return;
}
- an error not easily discovered
- An error was discovered processing the <wsse:Security> header
- UE4:ICU data directory was not discovered
- Easily refresh an UpdatePanel, using JavaScript
- Substitution Failure Is Not An Error
- Substitution failure is not an error
- NI Vision error : not an image解析
- ERROR in AppModule is not an NgModule
- Error creating index WriteConcern detected an error 'not master'
- one case of Hibernate error:could not load an entity
- Cordova Error: Could not find an installed version of Gradle
- Latex error "Not enough room in an internal buffer" 解决方法
- ORA-15063: ASM discovered an insufficient number of disks for diskgroup "ASM,KEL"
- VB.NET Error Object reference not set to an instance of an object
- SQL Error:An aggregate may not appear in the set list of an UPDATE statement.
- Submit Reporting Table Error : Object reference not set to an instance of an object.
- 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- not an error (code 0): Could not open the database in read/write mode.的解决办法
- MD /MDD /ML /MT /MTD的简单介绍
- JDK源码分析之Set类详解——适配器模式的应用
- WITH 查询
- WIFI手动命令行连接AP
- System.Drawing.Imaging 命名空间
- an error not easily discovered
- truncate table ERROR:ORA-02266 表中的唯一/主键被启用的外键引用
- Sip 响应状态码 对照 详解
- Spring AOP 详解
- 查询采购接收信息服务& 查询付款信息服务
- 一。ext4.0 树菜单链接类似选项卡 demo[非MVC模式]
- javacc lookahead 全面解析
- Postgres 管理
- EAS F7多选属性控制