Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert char *aa="test" to unsigned char[]={0x74 ,0x65 ,0x73 ,0x74} in C
Posted

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

C
#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;
}
 
Share this answer
 
You asked for C so the cast is C. cout is easier than printf to show contents of array.

C++
#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;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900