一道华为公司的面试题目.怎么把字符串转化为字节数组?
比如 CString *str="ABCD";(长度不确定的字符串) (VC中) 或 char *str="ABCD";(长度不确定的字符串) (C语言中)
变为 byte DATA[4]; (要求字节数组长度应和字符串一致)
DATA[0]='A';
DATA[1]='B';
DATA[2]='C';
DATA[3]='D';
解:
//编译没通过
#include<iostream>
#include "stdlib.h"
using namespace std;
int exch(char *s);
main()
{
char * s="abcdefg";
exch(s);
system("pause");
return 1;
}
int exch(char *s)
{
int n,i;
n=strlen(s);
char data[n+1];
if(*s='/0')
return;
for(i=0;i<=n;i++,s++)
data[i]=*s;
cout<<data<<endl;
}