# Thread: Converting a Long to chars[4]

1. ## Converting a Long to chars[4]

Is there any way to convert a long number to an array of 4 chars so that the four bytes comprising that long can be looked at individually? For example, if I have a color that is 0x00FFFFFF (white), is there a way to treat this as though it were char[4] = {0xFF, 0xFF, 0xFF, 0xFF}?

Russ

2. I have done something similar, code below. However my array type data (pBGR) is an array of only 3 bytes.

Alternatively you could do a memcpy from your integer to your array, however, this does not take into account platform specific byte ordering differences dependencies.

Code:
```BtCol& BtCol::operator =(TColor rhs)
{
// Assignment
int rgb = ColorToRGB(rhs);
pBGR[BGR_BLUE] = ((rgb & 0xFF0000) >> 16);
pBGR[BGR_GREEN] = ((rgb & 0xFF00) >> 8);
pBGR[BGR_RED] = (rgb & 0xFF);
return *this;
}```

3. Code:
```union u {
long l;
char c[4];
};```
Then:

union u test;

test.l = 1234567879;
test.c[0] ... test.c[3] contain your bytes.

Quzah.

4. Originally posted by Davros
I have done something similar, code below. However my array type data (pBGR) is an array of only 3 bytes.
Code:
```BtCol& BtCol::operator =(TColor rhs)
{
// Assignment
int rgb = ColorToRGB(rhs);
pBGR[BGR_BLUE] = ((rgb & 0xFF0000) >> 16);
pBGR[BGR_GREEN] = ((rgb & 0xFF00) >> 8);
pBGR[BGR_RED] = (rgb & 0xFF);
return *this;
}```
Ah the joyst of incomplete code...

Quzah.

5. >Ah the joyst of incomplete code...

I posted it as an example. What you want you want the complete file, or the complete application?

Also, what is the size of 'long'? Has it occured to you that this is platform dependent?

6. Originally posted by Davros
>Ah the joyst of incomplete code...

I posted it as an example. What you want you want the complete file, or the complete application?

Also, what is the size of 'long'? Has it occured to you that this is platform dependent?
No you didn't. Your code calls functions which are not defined or otherwise clarified.

As to platform dependance, it doesn't take a brain surgeon to fix this so it works everywhere:
Code:
```union u
{
long l;
char c[sizeof(long)];
};```
Or if I really felt like it, I could make a this a class that would do all the work for you. If you really want to be anal about it, you could throw in a division by sizeof char.

On a side note, the question was how to convert to four characters, so my initial answer was correct in the first place. As such, platform independance is irrelevant.
[/edit]

Quzah.

7. another way is
Code:
```long foobar = 0x22334455;
char *foostuff = &long;
// now foostuff[0] through foostuff[3] have the individual bytes```
which is doing exactly the same thing as quzah's example.

8. >No you didn't.

How the do you know why I posted it? I don't deserve this. The guy asked for help & I replied. He's free to ignore it if he wants. I don't need any s**t from you.

>Your code calls functions which are not defined or otherwise clarified.

That's right. So?

>so my initial answer was correct in the first place. As such, platform independance is irrelevant.

If you like. You might not care which byte gets copied into which element, but I do. I was simply trying to explain why this might be important.

9. Originally posted by Davros
>No you didn't.

How the do you know why I posted it? I don't deserve this. The guy asked for help & I replied. He's free to ignore it if he wants. I don't need any s**t from you.
I mean, no you didn't post complete code. You implied that you did, I stated that you didn't.

Originally posted by Davros
>Your code calls functions which are not defined or otherwise clarified.

That's right. So?
They can't understand how to simply map four bytes to an array, and you expect them to understand your incomplete code containing bit shifting and random unclarified functions?

They asked how to span a long across four bytes, not how to decypher some random gibberish which isn't explained at all.

"Here, let me throw a bunch of masking and bitshifting at you, which is incomplete for what you want it for, and you can guess as to what's happening!"

Originally posted by Davros
>so my initial answer was correct in the first place. As such, platform independance is irrelevant.

If you like. You might not care which byte gets copied into which element, but I do. I was simply trying to explain why this might be important.
You didn't attempt to explain anything. You simply threw some masking and bitshifting at them. Furthermore, as you yourself stated, your example doesn't take into accoutn platform specifics, so why point it out in mine?

People like you tire me out.

Quzah.

10. >I mean, no you didn't post complete code. You implied that you did, I stated that you didn't.

No where did I state or say anything which implied my code was complete.

>They can't understand how to simply map four bytes to an array, and you expect them to understand your incomplete code containing bit shifting and randon unclarified functions?

May be you have a point here. I didn't explain about bit shifting. May be I thought it would be apparent, when may be it wasn't. As for my random unclarified functions, may be I didn't think they were relevant. May be I should have explained this & didn't.

However, your initial reply didn't explain much either. I didn't explain how bit shifting works & you didn't explain anything about unions.

>People like you tire me out.

You started this.

11. Davros... relax. Look at his site. 'nuff said.

12. Originally posted by golfinguy4
Davros... relax. Look at his site. 'nuff said.
Oh master of the repartee, please enlighten me. Do you require fancy pictures and graphics to bring light into your dull eyes? Or what specificly is your problem?

Quzah.

13. O quzah, please forgive me. I find that your GI Joe collection shows great refinement and maturity. BTW, do you collect barbies?

14. what exactly is immature about collecting something? you seem to be the one with a maturity problem; the last time i remember someone making fun of someone else because of the toys they owned/didn't own was somewhere around 4th-6th grade.

15. Originally posted by golfinguy4
O quzah, please forgive me. I find that your GI Joe collection shows great refinement and maturity. BTW, do you collect barbies?
We commonly refer to this as p____ envy. It's ok. You too can start your own collection for just a few hundred bucks. Naturally, it'll take a great deal more to hold a candle to my collection, but hey, you can try. That's barely a scratch on the surface of what I have. And yeah, I'm pretty damn proud of them.

On a side note, I don't collect Barbie, but if you will pull your head out of the sand, drop by eBay and spend two seconds browsing the auctions, you'll find litterally thousands of people who do. They tend to go for quite a bit of money.

If you can't understand the concept of collecting, why should I waste my time explaining the fun of it?

I imagine that I'm supposed to feign embarrasment or something because I have "toys". But then, if I did that, what would be the point of acknowledging I have them in the first place?

Like I said, p____ envy.

Quzah.