免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1572 | 回复: 1
打印 上一主题 下一主题

[C] strtol 在我的程序中有异常 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-05 22:22 |只看该作者 |倒序浏览
运行结果:

tmp[] = 2 1
21
tmp[] = d f
df
tmp[] = 6 e
6e
tmp[] = 5 a
5a
tmp[] = 0 9
9
tmp[] = e 4
e4
tmp[] = 9 1
91
tmp[] = d 0
d0
tmp[] = 1 4
4e

//从这里开始发生变化了,明明tmp[]={'1','4'},转换之后就成了 4e 了

test.c  代码如下
#include "str_to_int_array.c"
#include "stdio.h"
int main()
{
    int a = 0;
    char *str = "10 f3 07 b7 12 4d 06 9b 31 c9 f0 00 87 43 1a 67 c0 7f 9e 2c 13 b3 90 14 d0 91 e4 09 5a 6e df 21";
    guint8 *_str = NULL;
    int str_len = str_to_int_array(str,&_str);
g_print("str out :\n");
&nbsp;&nbsp;&nbsp;&nbsp;for (;a < str_len; a ++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(" %x",*(_str + a));
g_print("\n");
}


str_to_int_array  代码如下
#include "gtk/gtk.h"
#include "str_to_int_array.h"

gint str_to_int_array ( gchar *str_in, guint8 **int_out)
{
    gint loop, len, size ;
    gchar tmp[2];
    for (loop = 0, len = 0;  *(str_in + loop) != '\0' ; loop ++)            //先扫描一遍

    {                                                                                                                   //得到0-9,a-f,A-F

        if    (    ((*(str_in + loop) >= '0') && (*(str_in + loop) <= '9'))        //的个数

        ||        ((*(str_in + loop) >= 'a') && (*(str_in + loop) <= 'f'))   
        ||        ((*(str_in + loop) >= 'A') && (*(str_in + loop) <= 'F'))         )
        len ++;
    }
    size = (len + 1) / 2;
    len = size ;
    *int_out = g_malloc0 ( size );
    for (loop --; loop >= 0; loop-- )
    {
        if ( *(str_in + loop) != ' ' && *(str_in + loop - 1) != ' ' )
        {
            tmp[1] = *(str_in + loop-- );
            tmp[0] = *(str_in + loop );
            g_print ("tmp[] = %c %c\n",tmp[0],tmp[1]);
            size --;
            *(*int_out + size) = ( guint8 )strtol(tmp,NULL,16);
            g_print ("    %x \n" , *(*int_out + size));
        }
    }
    
    return len;
}


[ 本帖最后由 daschina 于 2009-5-5 22:24 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
2 [报告]
发表于 2009-05-06 09:11 |只看该作者
只见g_malloc0(),没见释放,内存泄漏吧, 别的没看
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP