View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
int32 to int array |
Posted: Wed Sep 09, 2020 3:06 pm |
|
|
I am trying to save an number int32 into an int array containing each of that number's int8
i.e. if I have
Code: | int32 number = 1599684516 which in HEX = 5F59 3FA4 |
how can I get to
where:
Code: | numberArray[0] = 0xA4;
numberArray[1] = 0x3F;
numberArray[2] = 0x59;
numberArray[3] = 0x5F; |
Any suggestions gratefully received. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Wed Sep 09, 2020 4:38 pm |
|
|
the make8() and make32() functions are one method of doing this (see them in the manual).
Additionally you can do a union type between the unsigned int32 and the 4 byte array. |
|
|
elcrcp
Joined: 11 Mar 2016 Posts: 62 Location: izmir / Turkey
|
|
Posted: Wed Sep 09, 2020 4:39 pm |
|
|
You can use make8() function, something like this would help you
Code: | for(int8 cnt=0;cnt<4;cnt++)
{
numberArray[cnt]=make8(number,cnt);
} |
_________________ There is nothing you can't do if you try |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Sep 09, 2020 4:58 pm |
|
|
I think he wants an 'unmake32()' function NOt a make32().
he needs to breakdown the int32 into 4 int8 ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Wed Sep 09, 2020 5:02 pm |
|
|
temtronic wrote: | I think he wants an 'unmake32()' function NOt a make32().
he needs to breakdown the int32 into 4 int8 ? |
yes, that's why we suggested make8() (I tossed in the make32 for rebuilding if desired). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Sep 10, 2020 12:25 am |
|
|
Honestly, use a union:
Code: |
union {
unsigned int32 whole;
unsigned int8 bytes[4];
} combiner;
combiner.whole=1599684516 //which in HEX = 5F59 3FA4
//then combiner.bytes[0] will contain 0xA4
//combiner.bytes[1] will contain 0x3F
//combiner.bytes[2] will contain 0x59, and
//combiner.bytes[3] will contain 0x5F
//The great thing about this is it'll work both ways. You can write (say)
combiner.bytes[2]=0x1A
//and then combiner.whole will contain 0x5F1A3FA4 = 1595555748
|
The union is a really nice way to move data from one format to another.
What happens is the two (or more) parts, are written into and read from
the same part of memory. So here you have an int32 variable called
'whole', in the same location as an array of four int8's. |
|
|
steve__p
Joined: 09 Sep 2020 Posts: 2
|
|
Posted: Thu Sep 10, 2020 12:07 pm |
|
|
what about this:
Code: |
int32 number;
int *numberarray;
numberarray=(int*) &number;
|
then you can use numberarray[0].... |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Thu Sep 10, 2020 2:21 pm |
|
|
steve__p wrote: | what about this:
Code: |
int32 number;
int *numberarray;
numberarray=(int*) &number;
|
then you can use numberarray[0].... |
That's kind of like the union, but the union is cleaner and you don't need to mess around with pointers. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Thu Sep 10, 2020 2:46 pm |
|
|
I'm thinking the union method is faster and smaller, though I haven't tested it.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Fri Sep 11, 2020 8:03 am |
|
|
You are using pointers either way (after all you access an array...). However
casting does have an overhead, so the pointer approach will take a little
longer.
If you use fixed indexes for the bytes, the union codes as single byte moves.
The pointer version even then results in a lot of code.
So the union will optimise better. |
|
|
|