Thread: byte to long

  1. #1
    Unregistered
    Guest

    byte to long

    ok i have a byte buffer

    byte buf[16]; in which i have stored longs.

    now how do i add the longs in it and get the total in another long?
    or in other words how would i read a long out of it?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You can't have longs in a byte buffer unless you use 4 byte slots for every long. In your case, you could store 4 longs in the array. Add them together using:

    long total=0;
    for(long i = 0; i < 4; i++)
    {
    total += (long*)(buf)[i];
    }

    I think I got it right.
    // Gliptic

  3. #3
    Unregistered
    Guest
    i came across this but i wasn't sure. becuase

    lets say i have a long thats something like this

    00010010 01011010 10010010 10010010

    when i save it to a
    byte buf[4]
    it then splites into four parts. if i add each byte that is buf[0] +buf[1] +buf[2] +buf[3] would not the answer be different?

    like:
    buf[0] = 00010010
    buf[0] = 01011010 +
    buf[0] = 10010010 +
    buf[0] = 10010010 +
    =================
    11001 0000 ??? not the same ???

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Of course it would be different. That's because you are adding all the bytes in the long together. If you want to read the long you just type: *(long*)buf .
    // Gliptic

  5. #5
    Unregistered
    Guest
    sorry could you give me an example?

    lets say i have a


    byte buf[16]; with four longs in it.

    to add should i use

    long total=0;
    for(long i = 0; i < 4; i+=4 )
    {

    total += * (long*)(buf)[i];

    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    No, this one:

    long total=0;
    for(long i = 0; i < 4; i++)
    {
    total += (long*)(buf)[i];
    }

    Think of the byte array as what it is, a byte array!
    There are 16 bytes in it. In this code, we are treating the array just as it was a long array with four elements.

    Sorry, I don't have time to write more now...
    // Gliptic

  7. #7
    Unregistered
    Guest
    thanks for your help, but i get this error

    '+=' : illegal, right operand has type 'long *'

    when using above, and if i remove the * the results still wrong?

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Here's how you'd do it with a byte array holding two longs -

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef unsigned char byte;
    
    int main ()
    {
       
    	byte buf[8]; 
    	
    	long a=10,b=25;
    
    	//Put two longs in byte array
    	*(long*)buf = a;
    	*(long*)&buf[4]=b;
    
    		
    	long total=0; 
    	for(int i = 0; i < 8; i+=4) 
    	{ 
    	total += *(long*)&buf[i]; 
    	} 
    
    	cout << total;
    	
    	return 0;
    }
    zen

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about

    Code:
    byte buf[16];
    long *lbuff = (long *)buff;
    for ( i = 0 ; i < 4 ; i++ ) {
        sum += lbuff[i];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with using c++ class called with P/Invoke
    By dudeomanodude in forum C# Programming
    Replies: 2
    Last Post: 05-10-2009, 12:38 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  5. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM