Converting a Long to chars[4]

This is a discussion on Converting a Long to chars[4] within the C++ Programming forums, part of the General Programming Boards category; Is there any way to convert a long number to an array of 4 chars so that the four bytes ...

  1. #1
    Russell
    Guest

    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}?

    Cheers in advance,

    Russ

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

    [edit]
    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.
    Last edited by quzah; 12-17-2002 at 02:46 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    hello, internet!

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >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.
    Last edited by Davros; 12-17-2002 at 04:07 PM.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Davros... relax. Look at his site. 'nuff said.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    O quzah, please forgive me. I find that your GI Joe collection shows great refinement and maturity. BTW, do you collect barbies?

  14. #14
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    hello, internet!

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 12-18-2002 at 03:05 PM.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. long int type
    By sarahr202 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2009, 12:55 PM
  2. converting double to long int
    By gunshipolitico in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2005, 09:26 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21