5

Possible Duplicate:
C++ style cast from unsigned char * to const char *

I have the unsigned char* digest; which is the output of a program and i would like to pass it to a char* S1;

I type char* S1=digest; and does not work

Community
  • 1
  • 1
Hashed
  • 57
  • 1
  • 2
  • 6
  • 2
    The types are different so it’s not surprising that it doesn’t work. The question is: do you really need to declare `S1` as `char*`? Why not use `unsigned char*`? – Konrad Rudolph Sep 10 '12 at 12:20
  • Have you seen http://stackoverflow.com/questions/658913/c-style-cast-from-unsigned-char-to-const-char? – wmorrison365 Sep 10 '12 at 12:22
  • Actually I have a function which takes as input parameters char* and outputs unsigned char*... I would like to re-enter the output into the function again so I need to be of type char*... – Hashed Sep 10 '12 at 12:22
  • Some mid position in your `unsigned char * digest` is **'\0'** or **0** . That is the reason why the C-String when `reinterpret_cast(digest)` `(char *)digest` is truncated from 0 position to mid position. – Joma Jun 25 '17 at 03:51

4 Answers4

10

The simple answer: You need to cast it: reinterpret_cast<unsigned char*>(digest)

However, in this case you need to be aware that unsigned char* and char* are not really the same thing unless all elements in the array are less than 128.

char * either represents values from -128 to 127 (signed) or 0 to 255 (unsigned), the other always values from 0 to 255.

digest functions by their nature are likely to return values between 0 and 255, inclusive, in the result.

Also, the fact that the array might very well include null characters (the question does not really specify where the digest is coming from) many functions that accept char * or unsigned char* are likely to fail, since they assume the char* is a string (which it is not, really, if it is a binary digest of (presumably) fixed size)

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
perh
  • 1,668
  • 11
  • 14
2

It's because unsigned char and char (which is really signed char in your compiler) are different. You have to make an explicit typecast:

char* S1 = reinterpret_cast<char*>(digest);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    It would also happen on an implementation on which `char` is unsigned. The three char types are *always* three distinct types and there's never an implicit conversion between pointers-to-them, even though there are guaranteed only two distinct representations among the three of them. It's to stop you accidentally writing code that depends on whether `char` is signed or what representation signed integers have -- you can only write that code deliberately by casting. – Steve Jessop Sep 10 '12 at 12:24
  • 1
    I'd like to stress that char, signed char and unsigned char are in fact three distinct types. – sellibitze Sep 10 '12 at 12:49
1

Char types are layout compatible, so if you know what you're doing, you can simply force the pointer with a reinterpreting cast:

char * s1 = reinterpret_cast<char *>(digest);
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

C style cast: s1=( char *)digest;

C++ style cast: s1= reinterpret_cast<char *>(digest);

#include <iostream>
using namespace std ;



int main(void)
{

    unsigned char* digest;
    char * s1c,*s1cpp; 


    //C style cast
    s1c=(  char *)digest;


    //C++ style cast
    s1cpp= reinterpret_cast<char *>(digest);




  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28