Codejie's C++ Space

Using C++

C++:找BUG~


    下面有个函数,用于将6个字节的字符数组‘转换’到12字节。例如:

src[6= 0x120x340x560x780x9a0xbc }    
dst[
12= '1','2','3','4','5','6','7','8','9','a','b','c' }

    因为目标是字符串风格,为了显示需要,dst实际使用的是13字节长度数组,如下:
unsigned char dst[13];

makeId(dst, src);

    函数如下,麻烦找下Bug。
void ISLUtil::makeId(unsigned char* id, const unsigned char* ptr)
{
    
int i = 0;
    
while(i < 6)
    
{
        unsigned 
char t = *ptr >> 4;
        
if(t >= 0 && t <= 9)
        
{
             
*id = t + '0';
        }

        
else if(t >= 0x0a && t <= 0x0f)
        
{
            
*id = t + 'a';
        }

        
else
        
{
            
*id = 0;
        }

        
        t 
= (*ptr & 0x0F);
        
++ id;
        
if(t >= 0 && t <= 9)
        
{
            
*id = t + '0';
        }

        
else if(t >= 0x0a && t <= 0x0f)
        
{
            
*id = t + 'a';
        }

        
else
        
{
            
*id = 0;
        }

        
        
++ ptr;
        
++ id;
        
++ i;
    }

    id[
12= '\0';
}


<----郁闷的分割线---->

    Y的,白痴的错误搞了我两天。。。。

posted on 2011-01-18 17:10 codejie 阅读(1973) 评论(18)  编辑 收藏 引用 所属分类: C++随笔而已

评论

# re: C++:找BUG~[未登录] 2011-01-18 18:24 kkk

把两个 *id = 0都改成
*id = '0';  回复  更多评论   

# re: C++:找BUG~ 2011-01-18 18:25 hello

*id = t + 'a';
应为
*id = t - 10 + 'a';

封装
inline unsigned char digit2char(unsigned char d)
{
assert(d < 0x10);
if (d < 10)
{
return d +'0';
}
else
{
return d - 10 + 'a';
}
}  回复  更多评论   

# re: C++:找BUG~ 2011-01-18 18:35 w

inline unsigned char digit2char(unsigned char d)
{
assert(d < 0x10);
return "0123456789abcdef"[d];
}  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 18:55 Lucifer

#include<stdio.h>
unsigned char src[6] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
void makeId(unsigned char* id, const unsigned char* ptr)
{
int i = 0;
while(i < 6)
{
unsigned char t = *ptr >> 4;
if(t >= 0 && t <= 9)
{
*id = t + '0';
}
else if(t >= 0x0a && t <= 0x0f)
{
*id = t + 'a'-10;
}
else
{
*id = 0;
}

t = (*ptr & 0x0F);
++ id;
if(t >= 0 && t <= 9)
{
*id = t + '0';
}
else if(t >= 0x0a && t <= 0x0f)
{
*id = t + 'a'-10;
}
else
{
*id = 0;
}

++ ptr;
++ id;
++ i;
}
*id = '\0';
}
int main()
{
unsigned char dst[13];

makeId(dst, src);
puts((char*)dst);
return 0;
}  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 21:25 codejie

@kkk
恩,可以这样。但这里设置为0,而不是‘0’,用于代表出错情况。  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 21:26 codejie

@hello
是的。一看就是搞C++的,喜欢封装。。。  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 21:28 codejie

@w
太有创意了。。  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 21:31 codejie

@Lucifer
要是按照中国教育标准看,这个是标准答案。。。我是找了两天才发现。。。
最关键是就是最后的那个--‘*id = '\0';‘我直接id[12]=0,导致覆盖其他数据了。。。白痴啊我。。。  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-18 21:31 codejie

@all
辛苦各位帮忙看代码了,谢谢啊~  回复  更多评论   

# re: C++:找BUG~ 2011-01-19 02:34 电脑知识与技术博客

哈哈哈,做事要细心啊  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-19 10:01 haha

据说在每一个软件公司里,都有一个扫地的老太太。很偶然地,当她经过一个程序员的身边,扫一眼屏幕上的代码,会低声提醒对方说:小心,缓冲溢出了。  回复  更多评论   

# re: C++:找BUG~ 2011-01-19 10:54 codejie

@haha
您好,扫地老太太~哈哈~  回复  更多评论   

# re: C++:找BUG~ 2011-01-19 11:10 abeng

字符数组在初始化时必须有'\0'结束吧?  回复  更多评论   

# re: C++:找BUG~[未登录] 2011-01-19 11:50 Max

我手頭上也有一些問題要提問...能夠幫我找下嗎? thanks.
Q1: 下列那邊有問題?

class A
{
A(){};
protected:
double _a;
public:
A(double a) : _a(a) {}

double eval(double a1)
{
return _a * a1;
}
virtual ~A(){}
};

class B : public A
{
public:
double eval( double b )
{
_a = _a * b;
return _a;
}
};

class C
{
public:
C() : _myA(0) {}
double _a;
A* _myA;
~C()
{
if ( _myA != 0 )
delete _myA;
}
};

class D : public A, public C
{
public:
int run(double d) { return d + _a; }
};

Q2:以下找出錯誤跟可能的危險

void main()
{
A* testB = new B;
double testVal = b.eval( 4.0 );
C testC;
testC._myA = new A(4);
C testC2 = testC;
D testD;
int testVal2 = testD.run( 3.0 );
delete testB;
}

Q3:以下程式那邊造成complier有問題? 可以保留SetValue的Const-ness嗎? 如果可以,怎麼修改?
class cTest
{
public:
cTest(){};
~cTest(){};

void SetValue( const double val ) const
{
m_value = val;
}

private:
double m_value;
};

Q4:以下程式是否有memory leak問題, 如果有,怎麼避免?
class cTest
{
public:
cTest(){};
~cTest(){};

private:
double m_double[100];
};

void function( cTest* test )
{
delete test;
test = new cTest();
}

void main( void )
{
cTest *t = new cTest();
function( t );
delete t;
}  回复  更多评论   

# re: C++:找BUG~ 2011-01-19 12:23 codejie

@abeng
这个到不一定,要看实际需要了,毕竟并不是所有的字符数组要做字符串用的,也许就是个字符集会呢?  回复  更多评论   

# re: C++:找BUG~ 2011-01-19 12:35 codejie

@Max
兄弟,你这不是找bug啊,是在出考试题吧。。。  回复  更多评论   

# re: C++:找BUG~ 2011-01-26 12:36 xiang_kgd

for ( i = 0; i < 6; i++){
sprintf(dest[i*2],"%02x",(unsigned char)src[i]);
}  回复  更多评论   

# re: C++:找BUG~ 2011-01-26 12:55 codejie

@xiang_kgd
dest前面要不要加个&号?话说,我不喜欢sprintf...  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜