字符串 转 unsigned int 的问题

yaya237 2014-06-04 03:01:09
对于移位运算很晕,现在想把一个字符串转成整型数组,不知道该怎么写,谢谢大神给解!
比如:
char instr[]="044B04320430044B“;
转成
usigned int instr[]={0x044B,0x0432,0x0430,0x044B};
...全文
761 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-06
  • 打赏
  • 举报
回复
将你放在工程中的代码片断帖上来。
yaya237 2014-06-06
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
作为一个C程序员,对 scanf,sscanf,fscanf printf,sprintf,fprintf 这类函数的用法,还是要做到“拳不离手,曲不离口”的。
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
    p=s;
    while (1) {
        k=sscanf(p,"%d%n",&v,&n);
        printf("k,v,n=%d,%d,%d\n",k,v,n);
        if (1==k) {
            p+=n;
        } else if (0==k) {
            printf("skip char[%c]\n",p[0]);
            p++;
        } else {//EOF==k
            break;
        }
    }
    printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
是不是sscanf 函数对编译环境还有要求呢?我在VC++中调试好,没有问题,可一放到工程中调用就不行了,嵌入式系统不能用么?
阿桑- 2014-06-06
  • 打赏
  • 举报
回复
学习了!每天这么累居然还有心情给别人改错误,是不是有的大神在代码里找错误的感觉就像当初马克思写资本论时写累了做微积分题目的感觉。每天有你们这些大神在,学习的路上永远不会孤独!
赵4老师 2014-06-06
  • 打赏
  • 举报
回复
sscanf(pcASC+i*4,"%4hX",pcHex+i);
  • 打赏
  • 举报
回复
明白原理是关键,如果不明白原理,会很费解的。
yaya237 2014-06-06
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
将你放在工程中的代码片断帖上来。
请忽略pbm_ucs2_to_ucs2_81 中后半部分,测试加的比较乱 test_LOG("pbm_ucs2_to_ucs2_81 instrlen 1 %d",instrlen); for(i=0;i<instrlen;i++) { test_LOG(" outstr_tmp[%d] %02x",i,*(outstr_tmp+i)); } instrlen=wcslen(outstr_tmp2); test_LOG("pbm_ucs2_to_ucs2_81 2 %d",instrlen); ....... }
yaya237 2014-06-06
  • 打赏
  • 举报
回复
boolean pbm_ucs2_to_ucs2_81(uint8 *instr,uint8 *out_octets,uint16 outlen) { uint16 i=0; uint16 instrlen=0; uint16 outstr_tmp[(PBM_TEXT_SIZE_BYTES/4)+1]={0}; test_LOG("instr %s len is %d",instr,strlen(instr)); test_asc_to_uint16_hex(outstr_tmp,instr,strlen(instr)); instrlen=wcslen(outstr_tmp); test_LOG("pbm_ucs2_to_ucs2_81 instrlen 1 %d",instrlen); for(i=0;i<instrlen;i++) { test_LOG(" outstr_tmp[%d] %02x",i,*(outstr_tmp+i)); } instrlen=wcslen(outstr_tmp2); test_LOG("pbm_ucs2_to_ucs2_81 2 %d",instrlen); ....... } uint32 wcslen(const uint16 *ucs2_str) { uint32 retval = 0; /* Value to be returned. */ if (ucs2_str) while (*ucs2_str++) { retval++; } /* Increment retval until 0x0000 */ return retval; /* Return what we counted. */ } void test_asc_to_uint16_hex (uint16 *pcHex, char *pcASC, unsigned int iLength) { int i; test_LOG("test_pbm:test_asc_to_uint16_hex"); for (i=0;i<iLength/4;i++) { sscanf(pcASC+i*4,"%4X",pcHex+i); test_LOG("test_pbm:pcHex+%d =%02x",i,*(pcHex+i)); } } 现在打印出的log信息 instr 04320430044B043204300432043F04320430044B04320430 len is 48" test_pbm:test_asc_to_uint16_hex" test_pbm:pcHex+0 =432" test_pbm:pcHex+1 =00" test_pbm:pcHex+2 =44b" test_pbm:pcHex+3 =00" test_pbm:pcHex+4 =430" test_pbm:pcHex+5 =00" test_pbm:pcHex+6 =43f" test_pbm:pcHex+7 =00" test_pbm:pcHex+8 =430" test_pbm:pcHex+9 =00" test_pbm:pcHex+10 =432" test_pbm:pcHex+11 =00" 每隔一个不正常
赵4老师 2014-06-05
  • 打赏
  • 举报
回复
作为一个C程序员,对 scanf,sscanf,fscanf printf,sprintf,fprintf 这类函数的用法,还是要做到“拳不离手,曲不离口”的。
#include <stdio.h>
char s[]="123 ab 4";
char *p;
int v,n,k;
void main() {
    p=s;
    while (1) {
        k=sscanf(p,"%d%n",&v,&n);
        printf("k,v,n=%d,%d,%d\n",k,v,n);
        if (1==k) {
            p+=n;
        } else if (0==k) {
            printf("skip char[%c]\n",p[0]);
            p++;
        } else {//EOF==k
            break;
        }
    }
    printf("End.\n");
}
//k,v,n=1,123,3
//k,v,n=0,123,3
//skip char[ ]
//k,v,n=0,123,3
//skip char[a]
//k,v,n=0,123,3
//skip char[b]
//k,v,n=1,4,2
//k,v,n=-1,4,2
//End.
还差得远呢 2014-06-05
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
char str[]="044B04320430044B“;
usigned int  is[4];//{0x044B,0x0432,0x0430,0x044B};
int i;
for (i=0;i<4;i++) sscanf(str+i*4,"%4X",&is[i));
yaya237 2014-06-05
  • 打赏
  • 举报
回复
引用 7 楼 ForestDB 的回复:
[quote=引用 6 楼 yaya237 的回复:] [quote=引用 1 楼 starytx 的回复:] 先将字符串4个一组的截取出来,然后将每一组用atoi转换
atoi 转出来的是16进制的么?[/quote] 这个问题就有问题,int无所谓16进制 int a = 1; int b = 0x1; a和b就是一回事。[/quote] 弱弱的再问一下,但是0x1234 跟1234 不一样吧?
ForestDB 2014-06-04
  • 打赏
  • 举报
回复
老赵的方法就很好。
ForestDB 2014-06-04
  • 打赏
  • 举报
回复
引用 6 楼 yaya237 的回复:
[quote=引用 1 楼 starytx 的回复:] 先将字符串4个一组的截取出来,然后将每一组用atoi转换
atoi 转出来的是16进制的么?[/quote] 这个问题就有问题,int无所谓16进制 int a = 1; int b = 0x1; a和b就是一回事。
yaya237 2014-06-04
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
先将字符串4个一组的截取出来,然后将每一组用atoi转换
atoi 转出来的是16进制的么?
水平不流 2014-06-04
  • 打赏
  • 举报
回复
引用 4 楼 ak47_wz 的回复:
[quote=引用 1 楼 starytx 的回复:] 先将字符串4个一组的截取出来,然后将每一组用atoi转换
+1[/quote] 回复错了。。 我以为是赵老师那一层的。。。不需要这样子做啊,直接sscanf,然后格式就行了。
水平不流 2014-06-04
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
先将字符串4个一组的截取出来,然后将每一组用atoi转换
+1
赵4老师 2014-06-04
  • 打赏
  • 举报
回复
for (i=0;i<4;i++) sscanf(str+i*4,"%4X",&is[i]);
赵4老师 2014-06-04
  • 打赏
  • 举报
回复
char str[]="044B04320430044B“;
usigned int  is[4];//{0x044B,0x0432,0x0430,0x044B};
int i;
for (i=0;i<4;i++) sscanf(str+i*4,"%4X",&is[i));
starytx 2014-06-04
  • 打赏
  • 举报
回复
先将字符串4个一组的截取出来,然后将每一组用atoi转换

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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