0

I have this string.

char * data = "A1B2C3D4E5F6A7B8";

Here each A1 B2 C3 D4 E5 F6 A7 B8 will be bytes for unsigned char* buffer.

My concern is how to convert the data into unsigned char* buffer.

unsigned char* buffer;
buffer = (unsigned char*)data;

I will use the buffer for a parameter of a function like this to write the data into memory.

int abc(uint64_t address, buffer, uint32_t lenght);

Can anyone please give me a correct way?

KKS
  • 1,389
  • 1
  • 14
  • 33

1 Answers1

0

Here it is a demo about the char* buffer, unsigned char* and string.

// test.cpp
#include<iostream>
using namespace std;
void fun(unsigned char* pt) {
   cout<<*pt<<*(pt+1)<<endl;  // print first and second element
}
int main(){
    char Data[] = "abcde";       // a string with 6 elements stored in char array.
    char* data = Data;           // char pointer point to the char array
    unsigned char* buffer;       // declare an unsigned char
    buffer = (unsigned char*) data;  // buffer points to the same address data pointed.
    fun(buffer);                 // function requires an unsigned char pointer
    return 0;
}

The output

ab

To compile and run

g++ -Wall test.cpp;
./a.out

P.S.

char * data = "A1B2C3D4E5F6A7B8";

The above line would cause a warning when compiling because the string is immutable. Better changes it to char const * data = "A1B2C3D4E5F6A7B8";

Xin Cheng
  • 1,432
  • 10
  • 17