string to char conv

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Aug 14 00:24:53 UTC 2018


On Monday, August 13, 2018 6:06:22 PM MDT zeus via Digitalmars-d-learn 
wrote:
> i have the following code in d and i get as result 4D77EB, also i
> have the following code in c++ wich give me as results
> 0xABCDEF123abcdef12345678909832190000011111111 how i can get in d
> 0xABCDEF123abcdef12345678909832190000011111111 instead of 4D77EB
>
>
> // D
>
> void test(string test){
>   char* testi = cast(char*)(test);
>   writeln(testi);
>
> }
>
>
> void main()
> {
>   test("0xABCDEF123abcdef12345678909832190000011111111");
> }
>
>
> // C++
>
> void test(string str){
>      const char* testi = str.c_str();
>      printf("%s\n", testi);
>
> }
>
> int main(int argc, char const *argv[]){
>
>      test("0xABCDEF123abcdef12345678909832190000011111111");
> }

Why are you casting the string to a char*? That's just going to make writeln
print out the pointer value. If you want to print out the value of the
string, then just pass the string to writeln.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list