-
January 13th, 2009, 02:55 PM
#1
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
-
January 13th, 2009, 03:27 PM
#2
Re: Copying char * to a dynamic array of ints
Originally Posted by rudyloo
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
-
January 13th, 2009, 03:30 PM
#3
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?
-
January 13th, 2009, 07:41 PM
#4
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
-
January 13th, 2009, 09:27 PM
#5
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.
-
January 14th, 2009, 05:15 AM
#6
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...
- Guido
-
January 14th, 2009, 08:58 AM
#7
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.
-
January 20th, 2009, 01:55 AM
#8
Re: Copying char * to a dynamic array of ints
Originally Posted by GNiewerth
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|