LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-01-2005, 10:48 AM   #1
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Rep: Reputation: 15
reading raw data in to memory


ok i want to read 200kb of raw data.....
and put it in to memory as a real number
and i want access to that number so ican do calculations on it
like half it find the power and that lot
 
Old 07-01-2005, 10:55 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Not going to happen without a special library to perform calculations with numbers of that magnitude, and even then, it will take a LONG time to perform mathematical calculations with numbers like that.
 
Old 07-01-2005, 11:07 AM   #3
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
i just want to read 200kb of data (hexed) and put it in to decimal...which would result ina file that is 300kb
now i want to read that 300kb in to memory and access it as a number..so i can perform division on it
so i can find the power i need to get close to that file
 
Old 07-01-2005, 11:14 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
200kb of hex? That would be 100kb of raw (binary) data. Why do you need to find that power?

You'll need to write a program that uses the GNU Multiple Precision library, or something similar, to perform this math. If you intend to use it as an integer, you can use mpz_set_str(mpz_t val,char *string) with the value prepended with 0x and terminated in a null. You can use getc() to create this string in memory.
 
Old 07-01-2005, 11:17 AM   #5
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
ok i dont care about programming
i just want to know the lines of code to read raw data one byte a time..convert to dec and write the resulting code in to memory
and that give em a solution so i can access that number via a varible
 
Old 07-01-2005, 11:25 AM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I am not your personal programmer. Just to put this in memory would be several dozen lines of code. I will outline them here, for your benefit:
Code:
char *buf=(char *)malloc(200*1024+1);
unsigned int i=0;
FILE *fp=fopen("/path/to/file","r");
mpz_t val;

while(!feof(fp)){
    buf[i++]=fgetc(fp);
}
fclose(fp);
fp=NULL;
buf[i]=0;

mpz_init(val);
mpz_set_str(val,buf,16);
val now contains the value you were looking for. That code is just a skeleton, you'll obviously need some error checking and other work.
 
Old 07-01-2005, 11:38 AM   #7
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
char *buf=(char *)malloc(200*1024+1);
unsigned int i=0;
FILE *fp=fopen("/path/to/file","r");
mpz_t val;

while(!feof(fp)){
buf[i++]=fgetc(fp);
}
fclose(fp);
fp=NULL;
buf[i]=0;

mpz_init(val);
mpz_set_str(val,buf,16);

so if i did newval = val / 2 (i know that not probably right) but that would give me a val in newval which is half the size....which that all i would have to do is this newval x 2 = val
 
Old 07-01-2005, 11:54 AM   #8
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
so thats it....i dont actually get any help aprt from some code that i dont know what it is for..i assume it is either C or C++
but dunno if the idea of just halving value will result in it being smaller in generally no mater what it is
 
Old 07-01-2005, 12:03 PM   #9
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
What kind of help do you want? I pointed you to the proper libraries and functions, and gave you sample code to read in the value. With that value, you'll have to use the GNU MP library's functions to manipulate it.
 
Old 07-01-2005, 12:17 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
dunno if the idea of just halving value will result in it being smaller in generally no mater what it is
smaller in size or in value ?
 
Old 07-02-2005, 08:15 AM   #11
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
just say u could make a number turn in to a base that supported 300k....i could represent any 300kb number with very few numbers yes......ok i want to know if i could just tell u any number in sequence.....and u could work it out...e.g. 3000 digit ..now u tell me what info u need to determine a number by cubing ..
 
Old 07-02-2005, 12:20 PM   #12
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
How could you represent and 300 kb number with very few numbers? I'm a bit confused.
 
Old 07-02-2005, 12:49 PM   #13
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Ok first your question is really badly written.

1. you dont ask what programming language you need help in so we assumed it was C because of its ablity to manipulate memory easly.


the code written here is C if im mistaken somone correct me please. Thats a nice bit of code and does answer your question. It however is not compleate (not my words but the words of the author) and should not be used in its pure form but compleated with proper error checking.


now even if 300kb of raw data doesent seam like alot as far as modern space on computers go it acutally is a huge amount of data when working with raw binary data in programming.


please rewrite your question with some details on what you are trying to accomplish and we will be happy to assist you in any way.

Im not trying to be mean or anything but your question is kinda just thrown out there so you get erratic results.
 
Old 07-02-2005, 01:15 PM   #14
whizbit
Member
 
Registered: Jul 2005
Location: uk
Distribution: mandriva....not out of choice
Posts: 65

Original Poster
Rep: Reputation: 15
ok u use 16 to represent anynumber in binary .......if thats is yes
then how many numbers is it actually representing if i use base 32
 
Old 07-02-2005, 01:25 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Base 32? That would be 5 bits/character, so 300k would become 5/8 of that, or about 187k.

Exvor, you are correct in all parts of what you said. I'm glad to know I wasn't just so confusing that nobody could understand me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
writing raw data to a tiff file James_dean Programming 4 10-25-2005 05:03 AM
Reading raw hard disk sectors villie Programming 4 05-13-2004 12:18 AM
Capturing raw mouse data pld Linux - Software 4 04-11-2004 08:13 PM
Raw Syn Packet with Data GodSendDeath Programming 4 04-06-2004 04:53 PM
Raw Packet Data vanibhat Linux - Security 1 08-01-2003 07:42 AM

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

All times are GMT -5. The time now is 06:48 PM.

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