Convert char* to WORD*

This is a discussion on Convert char* to WORD* within the C Programming forums, part of the General Programming Boards category; I need to convert a char* to a unsigned int* for input into an RC5 encryption algorhythm method. The program ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    20

    Convert char* to WORD*

    I need to convert a char* to a unsigned int* for input into an RC5 encryption algorhythm method. The program ends up crashing when trying to use the converted type. How can this conversion be made properly?
    Code:
    #define WORD unsigned int;
    .
    .
    char* pt = "plaintext";
    WORD* plaintext = (WORD*) pt;
    //do encryption

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well you certainly can't just cast two data types that are of different sizes and expect something to magically make sense of it. What I suspect you want to do is convert each character in your char array to it's corresponding integer ASCII code value, and put all of these values in an unsigned array.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You need to copy the entire char array into a WORD array of the same length. Converting a single char to an unsigned int is not a problem since a char fits well within an int.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    20
    OK I got it, appreciate your help.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Crash is likely due to alignment because a char can be located at any byte boundary but an int has to be at a word boundary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [33.8] Can I convert a pointer-to-function to a void*?
    By EVOEx in forum C++ Programming
    Replies: 40
    Last Post: 08-19-2009, 07:50 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

Tags for this Thread


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