convert unsigned char to char *

This is a discussion on convert unsigned char to char * within the C++ Programming forums, part of the General Programming Boards category; OK I have a Code: char *arg which points to a char msg[1024] and I have a Code: unsigned char ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    convert unsigned char to char *

    OK I have a

    Code:
    char *arg which points to a char msg[1024]
    and I have a

    Code:
    unsigned char test[1024];
    now I want to convert the char into a unsigned char

    Code:
    snprintf(test,sizeof(test),"%s",arg);
    But I get the error message error C2664: '_snprintf' : cannot convert parameter 1 from 'unsigned char [1024]' to 'char *'

    What can I do?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    all you have to do is typecast is. No other conversion necessary
    Code:
    snprintf((char*)test,sizeof(test),"%s",arg);
    snprintf() is overkill when all you want to do is copy one string to another
    Code:
    strncpy((char*)test,arg,sizeof(test));
    or use std::string class and you don't have to worry about buffer overflow
    Last edited by Ancient Dragon; 03-03-2006 at 05:38 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    Quote Originally Posted by Ancient Dragon
    all you have to do is typecast is. No other conversion necessary
    Code:
    snprintf((char*)test,sizeof(test),"%s",arg);
    snprintf() is overkill when all you want to do is copy one string to another
    Code:
    strncpy((char*)test,arg,sizeof(test));
    or use std::string class and you don't have to worry about buffer overflow
    THX

    Could it be that in c or in old versions of c++ you don't have to care about convert a unsigned char to char *?

    My problem is that I have a blowfish .c file and a function int encrypt_string(char *key, char *str, char *dest, int len)
    but I have to give this function unsigned chars, because otherwise ascii codes above 127 get encrypt false. But my compiler strikes to give unsigned chars to this function.
    Last edited by keeper; 03-03-2006 at 05:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21