Solved

Extract a long int from a memory block..

Posted on 1999-01-28
7
202 Views
Last Modified: 2010-04-15
I am trying to extract the long int from a memory block
that looks something like this in Hex..

30 30 30 30 01 00 00 00 31 31 31 31 02 00 00 00 32....

I used a structure to access it like the following

Struct element {
    char   key[4];
    long   value;
}
Struct element array[20];

and retrieved the long with:
a = array[i].value;

I can no longer use this method because the key[4] is now
of variable length...
Any ideas on how i can do this a quick way!

thanks..
0
Comment
Question by:sergelebel
7 Comments
 

Expert Comment

by:SPooK
ID: 1258480
the key[n] is of variable length? are you sure this works? the only way i can think of making a part of a structure variable-length is with using a pointer to a variable-length variable.
anyway, you could add another variable which stores the length of the variable key, so
you'd just have to add this value to the address of key to access the variable value.

0
 
LVL 32

Expert Comment

by:jhance
ID: 1258481
Struct element {
        char   key[4];
        long   value;
    }
    Struct element array[20];


No, key is NOT of variable length in this declaration.  It's of length 4.  Now if you have recoded this as:

Struct element {
        char   *key;
        long   value;
    }
    Struct element array[20];


I'll believe you but it still doesn't affect retrieving value.

There must be more to this than you've said....
0
 
LVL 1

Author Comment

by:sergelebel
ID: 1258482
I will explain it better!

I read in from a file an array of data..each element was fixed in size so i used a structure like I defined above to access the array...

but now the data I read from the file is not fixed in size.. the
KEY part is variable in length althought the VALUE part is still
a long int..there fore I am using a Pointer to locate the KEY and the VALUE..my problem is that  I don't know how to convert the
4 bytes pointed to by my pointer into a Long Int so that I could
printf("Value is %ld",*ptr);

hopes this makes sense!


0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 32

Expert Comment

by:jhance
ID: 1258483
In the example data you've given above: 30 30 30 30 01 00 00 00 31 31 31 31 02 00 00 00 32....

There is no way to know the length of the char[] data.  Usually strings like this are NULL terminated but in this case, they are not.  Very unusual.  There has got to be some marker or length associated with this string so that it's length can be determined.  Is there more data AFTER the value?

0
 
LVL 3

Accepted Solution

by:
rmichels earned 10 total points
ID: 1258484
In the example you gave printf("Value is %ld", &ptr) all you have to do is make sure ptr points to the start of the long value.  Printf does not care about the type of ptr.  the ld tells it how to interpret what its pointing to.

If you need to use that value in calculations then do

long *pLong;

pLong = (long *) ptr;

result = *pLong + 10; //example use

both of the above examples makes two assumptions.

1) ptr is pointing to the start of the long data
2) The long data was stored properly in the data to begin with
   [LOW WORD] [HIGH WORD]
   [LSB  MSB] [LSB  MSB ]



0
 
LVL 32

Expert Comment

by:jhance
ID: 1258485
rmichels,

I don't think you read the question.  You answer has no bearing on this situation.....
0
 
LVL 1

Author Comment

by:sergelebel
ID: 1258486
Rmichels!   you are right with the  long *lptr;   this as worked
                  quite well!  thank you!

JHance!    Thanks for your help..sorry if I confused you with my
                  explanation....I am able to figure out the length of
                  the char[] by searching for nulls in the long..since the                   max value never exceeds FF FF 00 00..
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

746 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

13 Experts available now in Live!

Get 1:1 Help Now