CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

int32 to int array

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 182

View user's profile Send private message

int32 to int array
PostPosted: Wed Sep 09, 2020 3:06 pm     Reply with quote

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
Code:

int numberArray[4]

where:
Code:
numberArray[0] = 0xA4;
numberArray[1] = 0x3F;
numberArray[2] = 0x59;
numberArray[3] = 0x5F;


Any suggestions gratefully received.
jeremiah



Joined: 20 Jul 2010
Posts: 1186

View user's profile Send private message

PostPosted: Wed Sep 09, 2020 4:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 09, 2020 4:39 pm     Reply with quote

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: 7625
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Sep 09, 2020 4:58 pm     Reply with quote

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: 1186

View user's profile Send private message

PostPosted: Wed Sep 09, 2020 5:02 pm     Reply with quote

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: 16320

View user's profile Send private message

PostPosted: Thu Sep 10, 2020 12:25 am     Reply with quote

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: 1

View user's profile Send private message

PostPosted: Thu Sep 10, 2020 12:07 pm     Reply with quote

what about this:
Code:

int32 number;
int    *numberarray;

numberarray=(int*) &number;

then you can use numberarray[0]....
dluu13



Joined: 28 Sep 2018
Posts: 354
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Thu Sep 10, 2020 2:21 pm     Reply with quote

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: 7625
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Sep 10, 2020 2:46 pm     Reply with quote

I'm thinking the union method is faster and smaller, though I haven't tested it.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 16320

View user's profile Send private message

PostPosted: Fri Sep 11, 2020 8:03 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group