• C

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..
LVL 1
sergelebelAsked:
Who is Participating?
 
rmichelsCommented:
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
 
SPooKCommented:
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
 
jhanceCommented:
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
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

 
sergelebelAuthor Commented:
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
 
jhanceCommented:
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
 
jhanceCommented:
rmichels,

I don't think you read the question.  You answer has no bearing on this situation.....
0
 
sergelebelAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.