Help, newbie question. Copy Long into an unsigned char array issue
Hello,
I am a C programming newbie. This is my first program and I have made it through quite well, researching tutorials on the web etc. I have a hurdle I just cannot figure out;
Code:
int main()
{
/* Ask for a position input */
bool ValueOk = false;
unsigned char Posn[4] = {0};
unsigned char Temp[1] = {0};
long double a;
long b;
int i;
int ulIdx;
do
{
cout << "Enter Desired Position (0.00 -> 195.00 mm): ";
scanf("%Lf", &a);
if (a >=0.00 && a<= 195.00)
{
if (a < 0)
{
b = floor(a * 100);
} else
{
b = ceil(a * 100);
}
printf("%02X", b);
Posn[0] = b; <---- PROBLEM
printf("%02X", Posn[0]);
/* Invert Order Of Bytes For abSendData */
for (ulIdx = 0; ulIdx < 4; ++ulIdx)
{
Temp[ulIdx + (3 - ulIdx)] = Posn[ulIdx];
}
for (i=0; i<4; ++i)
{
Posn[i] = Temp[i];
}
ValueOk = true;
}
} while (!ValueOk);
}
How do I copy the variable "b" (long), represented as (4) byte values, into the Posn[0, 1, 2, 3] array? I also, want to invert the order, hence the reason for the loop above. I want to then shuffle the (4) bytes in the Posn array, as Posn[3, 2, 1, 0]. How is this done? I've been playing with sprintf, without success. Would someone be kind enough to explain this to me?
Many thanks!