Solved

Convert array of bytes to signed longs

Posted on 2006-10-26
5
197 Views
Last Modified: 2010-04-15
I have program that calls on a library function to pull data off of a tape.  The function puts the data in a variable length unsigned char array where every 4 bytes represents a signed long integer.  How do I go about converting each 4 byte segment into signed longs (decimal values), and then back to a second unsigned char array (for testing purposes)?

Thanks.
0
Comment
Question by:mtvann
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17812719
Hi mtvann,

From the question it appears that there is no conversion involved ... bit sequence would be the same in both the cases ... If that is the case, then simply use a pointer to traverse the array ... Traversing signed long * and unsigned char * would achieve what you desire.

unsigned char buffer[BUFFER_LEN] = { /*your data */ };
...
unsigned char * ptr1 = buffer;
signed long * ptr2 = buffer;

while ( ptr1 < buffer+ BUFFER_LEN)
{
       //process *ptr1
       ptr1++;
}

//similar loop for ptr2

Cheers!
sunnycoder
0
 
LVL 5

Expert Comment

by:daohailam
ID: 17812794
IMO, you have to verify byte-order of the source data & your system first. Depend on byte-order (Big Endian or Little Endian), we will have the correct way to convert.  If they are the same byte-order, you can use following code:

unsigned char *data = getdatafromtape();
long *lp = (long *)data;

for (i = 0; i < N; i++)
   printf("%ld\n", *lp++);


HTH
0
 

Author Comment

by:mtvann
ID: 17815564
Hi daohailam,
   Byte order is the same... In the for loop, is N the length of data, or is it (for a 4 byte long) the length of data/4?

Thanks
mtvann
0
 
LVL 5

Accepted Solution

by:
daohailam earned 250 total points
ID: 17816503
N = length of data / 4

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17816563
For any given type, it would be

< BufferLength / sizeof(type)
If your buffer length was 6 and size of long is 4, two bytes would remain unread

If there is some remainder left, you would not be able to access it using the same pointer in the same loop. If you have to access those bytes, then you would have to copy them into a variable of that type. e.g. for longs

//End of loop ... ptr2 points to end of buffer - last few bytes remain which cannot be accessed since they would cause the
//loop to read beyond the end of array

if (BufferLength%sizeof (long))
{
       long k = strtol (ptr2, 0, 10);
        //process value in variable k
}

add error checking as required.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now