Solved

Convert array of bytes to signed longs

Posted on 2006-10-26
5
201 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

679 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