首页
社区
课程
招聘
[讨论]char*转为short方法,附方法。
发表于: 2010-1-18 11:31 10938

[讨论]char*转为short方法,附方法。

2010-1-18 11:31
10938
是否有方便的API之类的可以直接把char转为short[16位的]整数,这个char里要求符合十六进制的数。
要求是二个char变成一个ushort
比如12345678 要变成 12表示一个 34表示一个short....

下面是我的方法。休力活
把char数组里的内容,根据高位放在高位低位放在底位,转为十六进制存到申请内存的地方。

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
typedef unsigned short USHORT;
typedef USHORT *PUSHORT;
int main() 
{ 
	PUSHORT pus,pmp;
	USHORT pusss; //ushort 16bit
	int i,incnum;
	char *aa="3456ABCD";//这里要在十六进制的范围。测试这里要写大写字母
	i=strlen(aa);
	pus=(PUSHORT)aa;
	pmp=NULL;
	incnum=0;
	pmp=(PUSHORT)malloc(sizeof(USHORT)*(i+2));
	if (pmp==NULL)
	{
		printf("Error\n");
		return 0;
	}
//	RtlZeroMemory(pmp,i/2+1);
	memset(pmp,0,i+2);

	_asm
	{
		xor eax,eax
		mov edx,0 
		mov ebx,1//标志
		mov esi,[pus] //字符串地址
		mov edi,[pmp] //要写入的目的地址
		mov ecx,0
	
	comstart:
		mov al,BYTE PTR [esi]
		cmp al,41h
		jge completesm
		cmp al,39h
		jle completenum

	completesm:
		cmp al,46h
		jle comsmok


	completenum:
		cmp al,30h
		jge comnumok

	comnumok:
		sub al,30h
		cmp ebx,1
		jz editnumok
		or al,dl //高位与低位相或结果放在al中
		mov BYTE PTR [edi],al
		add edi,2
		inc esi
		inc ebx
		inc ecx
		cmp ecx,i
		jz exitloop
		jmp comstart
	comsmok:

		sub al,41h
		add al,10 //这里转为十六进制
		cmp ebx,1
		jz editsmok
		or al,dl //高位与低位相或结果放在al中
		mov BYTE PTR [edi],al
		add edi,2
		inc esi
		inc ebx
		inc ecx
		cmp ecx,i
		jz exitloop
		jmp comstart

	editnumok:

		mov dl,al
		shl dl,4
		inc esi
		dec ebx
		inc ecx
		cmp ecx,i
		jz exitloop
		jmp comstart

	editsmok:

		mov dl,al
		shl dl,4
		inc esi
		dec ebx
		inc ecx
		cmp ecx,i
		jz exitloop
		jmp comstart


exitloop:

	}
    while ((*pmp)!=0)
    {
		printf("%X\n",*pmp);
		pmp++;
    }


	return 0; 
} 

[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 178
活跃值: (144)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
..................................................................不知道该说啥了
2010-1-18 11:52
0
雪    币: 393
活跃值: (100)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
3
+++

char buf[] = "12345678";

ushourt = *(pushort)&buf;
2010-1-18 12:26
0
雪    币: 2105
活跃值: (424)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
4
mbstowcs
2010-1-18 12:28
0
雪    币: 109
活跃值: (10)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
5
[QUOTE=iiii;747502]+++

char buf[] = "12345678";

ushourt = *(pushort)&buf;[/QUOTE]

我要求是 12 作为一个short存放,34 也是,
直接这样转的话,只是把buf这个地址传给ushourt,然后再访问ushourt时,他会把1234作为一个short
2010-1-18 13:14
0
游客
登录 | 注册 方可回帖
返回
//