0

I am trying to convert a string pointer(I believe String^ is string pointer??) into a 2-digit hexdump string.
I have the main part working by utilizing sprintf to print the hex value into a buffer and copy it into the hexdump char array secretHex.
However, I was not able to convert the char array into String, it only display 'True' when I try printf the converted string.
Can someone give me an idea of what is happening at the end?
I would like to be able to print out the result hex as the initial String^ secretKey.
Thanks!!

String^ secretKey = "1122334455667788";
printf("1. %s\n", secretKey);
int n = 0;
char buffer1[17], buffer2[3], secretHex[33];

sprintf_s(buffer1, "%s", secretKey);
while (n < sizeof(buffer1) - 1) {
    char value = buffer1[n];
    sprintf_s(buffer2, "%02x", value);
    secretHex[2 * n] = buffer2[0];
    secretHex[2 * n + 1] = buffer2[1];
    n++;
}
secretHex[sizeof(secretHex)-1] = '\0';
printf("The secretHex key is %s\n", secretHex);
String^ hex = System::Convert::ToString(secretHex);
printf("2. %s\n", hex);

//OUTPUT
1. 1122334455667788
The secretHex key is 31313232333334343535363637373838
2. True
Joseph Chien
  • 47
  • 1
  • 6
  • 2
    This is not C++ – Sean F Oct 12 '16 at 05:53
  • What type of language should I tag it with then? I am building base on someone else's work and I have been mainly researching on C++. Thanks! – Joseph Chien Oct 12 '16 at 06:09
  • Reason for using String^ is for I am taking a String^ object in and return for the conversion, and I am really rusty with C-like language now. Have not touch it for a long while. – Joseph Chien Oct 12 '16 at 06:12
  • 1
    `String^` is either [C++/CLI](https://en.wikipedia.org/wiki/C%2B%2B%2FCLI) or the older [Managed C++](https://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B), both of which are Microsoft extensions that run on the .NET framework. Standard C++ doesn't ever use a caret after a data type like that. `System::Convert::ToString` is also a .NET function. – Wyzard Oct 12 '16 at 06:16

1 Answers1

0

I have found a solution on stackoverflow after realizing I am dealing with C++/CLI instead of C++.

char* pstr = &secretHex[0];
String^ hex = gcnew String(pstr);
Community
  • 1
  • 1
Joseph Chien
  • 47
  • 1
  • 6