LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-04-2007, 12:44 PM   #1
Jose_Manu
LQ Newbie
 
Registered: Jan 2007
Posts: 1

Rep: Reputation: 0
Convert binary to integers and double array in C


Hi!

I have some binary files of the form:

number1, number2, number2 times double arrays of size number1

Now I can read the files, but I cannot convert the binary data into integer numbers and double arrays.

I would like to read the files and process the data in C. Could someone please give me some suggestions.

Thanks!

Jose
 
Old 01-05-2007, 07:14 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
This assumes that the buffer is not what you want to store the data in. All this does is change the way we look at the memory from unsigned chars to doubles.

Code:
extern unsigned char* buffer;

/*allocates new memory, which needs to be freed when finished with*/
double* create_double_array(unsigned char const * const buf, size_t const buf_size)
{
	double* f = malloc(buf_size);
	if(!f)return f;
	memcpy(f, buf, buf_size);
	return f;
}
/*read the size of the array from the buffer*/
int array_size = *((int*)buffer);

double* double_array = create_double_array(buffer+sizeof(int),array_size);
if(!double_array) printf("error could not create array");
Note: this assumes the binary file was created on a machine with the same endianness and assumes the data is correct.

Last edited by dmail; 01-05-2007 at 07:17 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
double matrix array in c alaios Programming 3 09-15-2005 11:34 AM
binary seach; vector instead of an array niteshadw Programming 1 08-25-2005 09:03 AM
c: array of poiners(integer or double) jetfreggel Programming 3 12-29-2002 08:39 AM
convert integers to ascii raven Programming 3 02-15-2002 03:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration