CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Copying char * to a dynamic array of ints

  1. #1
    Join Date
    Jan 2007
    Posts
    97

    Copying char * to a dynamic array of ints

    Hi

    Is it possible to populate a dynamic array of ints with data in a char*?

    I have a char *var that contains raw hex values. I would like to put that data into an dynamic array of ints where I can finally use the data for calculations.

    thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Copying char * to a dynamic array of ints

    Quote Originally Posted by rudyloo View Post
    I have a char *var that contains raw hex values.
    As opposed to a "cooked hex value"?

    What exactly is a "raw hex value"? A char holds a single byte of data valued from 0 to 255 when unsigned, and -128 to 127 when signed. Where does "hex" come into play?

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Copying char * to a dynamic array of ints

    Are you looking for 1 or 4 of the chars to end up in each int?

    If the latter, do you know or have control over the endianness of the bytes in the char array?

  4. #4
    Join Date
    Jan 2007
    Posts
    97

    Re: Copying char * to a dynamic array of ints

    Hi

    I basically have a char pointer to points to something like
    "71 0B 84 6F 00 00 70 0B 80 00 10 00 10 00 70 0B"

    I want to be able to copy these into an array. The goal actually is to copy 16bits given a offset. Therefore I really dont need to copy eveything into an array.

    Therefore if the offset was 1 16bit field, and I would want to copy 0x6F84.

    I do have control of the endian. I really havent thought about the endianness yet. Just trying to get something copied as step 1.

    Thanks

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Copying char * to a dynamic array of ints

    The question remains: If you're copying 0x6F84, do you want one int with value 28,548, or two with values 111 and 132? If the former, then you'll want memcpy(). If the latter, then a simple loop with assignment will do. In most ways a char is just an int with less storage space, after all.

    Well, that's assuming your bit in quotes above is a representation of the actual values in the chars, rather than a string. If that's a string then the problem is entirely different.

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Copying char * to a dynamic array of ints

    You can do something like this:

    Code:
    // pointer to raw hex data :p
    char *pcBuffer;
    
    // map pointer to short
    unsigned short* Ptr = reinterpret_cast<unsigned short*>( pcBuffer );
    
    // access 1st word
    unsigned short Word1 = *Ptr;
    
    // access 3rd word
    unsigned short Word3 = *(Ptr +2);
    Be careful with arrays of odd length...

  7. #7
    Join Date
    Jan 2007
    Posts
    97

    Re: Copying char * to a dynamic array of ints

    Lindley:

    For now I am copying int (Eg. 28548). I need to copy both methods eventually. I think its working

    CNiewerth:

    Thanks for that... i will try that out too, just to learn etc..


    Thanks all for your help.

  8. #8
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: Copying char * to a dynamic array of ints

    Quote Originally Posted by GNiewerth View Post
    You can do something like this:

    Code:
    // pointer to raw hex data :p
    char *pcBuffer;
    
    // map pointer to short
    unsigned short* Ptr = reinterpret_cast<unsigned short*>( pcBuffer );
    
    // access 1st word
    unsigned short Word1 = *Ptr;
    
    // access 3rd word
    unsigned short Word3 = *(Ptr +2);
    Be careful with arrays of odd length...

    I think it's dangerous to cast 'char array' into 'short/int' array. Char array can start at odd addresses and this will give segmentation fault on systems which require shorts to be half-word aligned and ints to be word-aligned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)