Link to home
Start Free TrialLog in
Avatar of sergelebel
sergelebel

asked on

Extract a long int from a memory block..

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..
Avatar of SPooK
SPooK

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.

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....
Avatar of sergelebel

ASKER

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!


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?

ASKER CERTIFIED SOLUTION
Avatar of rmichels
rmichels

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rmichels,

I don't think you read the question.  You answer has no bearing on this situation.....
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..