convert char * aa =" test" C中的unsigned char [] = {0x74,0x65,0x73,0x74} [英] convert char *aa="test" to unsigned char[]={0x74 ,0x65 ,0x73 ,0x74} in C

查看:81
本文介绍了convert char * aa =" test" C中的unsigned char [] = {0x74,0x65,0x73,0x74}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C

推荐答案

中将char * aa =test转换为unsigned char [] = {0x74,0x65,0x73,0x74} ,简单演员就足够了。

请注意 aa 应声明 const char *



As already noted, a simple cast is enough.
Please note aa should be declared const char *.

#include <stdio.h>

int main()
{
  const char * aa = "test";
  const unsigned char * bb = (const unsigned char *) aa;

  while ( *bb )
    printf("%02X ", *bb++);

  printf("\n");

  return 0;
}


你问C,所以演员阵容是C. cout比printf更容易显示数组的内容。



You asked for C so the cast is C. cout is easier than printf to show contents of array.

#include <iostream>
#include <conio.h>
#include <iomanip>


using namespace std;


int main()
{
char *aa="test";

unsigned char *bb = (unsigned char*) aa;

 int n = 0;

 while(bb[n])
 {
	 cout << hex << (int) bb[n] << endl;
	 n++;
 }


_getch();
return 0;
}


这篇关于convert char * aa =&quot; test&quot; C中的unsigned char [] = {0x74,0x65,0x73,0x74}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆