Curiosity of retrieving bytes of a data-type

This is a discussion on Curiosity of retrieving bytes of a data-type within the C++ Programming forums, part of the General Programming Boards category; Code: const int GetByte( void * i, unsigned char byte) { return ( int )((&i)[byte]); } The given function ( ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Curiosity of retrieving bytes of a data-type

    Code:
     
    const int GetByte(void* i, unsignedchar byte)
    {
       return(int)((&i)[byte]);
     
     
    }
    The given function ( i believe ) will return the specified byte of a given type. I have been using this to identify endian-ness, but I was wondering if there is a way to make this work on temporary.

    Such as
    Code:
    GetByte("A",0);
    
    
    

    will show the address, not the actual byte value
    Although
    Code:
    GetByte(1,0);
    works as expected.

    I am sure I am just blindly over-looking some simple logic, but I thought I would ask.

    Thank you sincerely.







    Last edited by Raigne; 04-27-2011 at 07:00 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    Code:
    const int GetByte(void* i, unsigned char byte)
    {
      return ( ( char* ) i )[ byte ];
    }
    you are taking the void* and using its address '&i' rather than i itself

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to define "Date" data type?
    By gogo in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2001, 02:33 AM
  2. Retrieving BIOS password
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-14-2001, 11:55 PM
  3. conversion for binary data type
    By DMaxJ in forum C++ Programming
    Replies: 2
    Last Post: 10-23-2001, 01:57 PM
  4. Curiosity Question
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2001, 04:24 PM
  5. Just out of curiosity
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-10-2001, 01:44 PM

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