Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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!

1 Solution
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....
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 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!

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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?

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
   [LSB  MSB] [LSB  MSB ]


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

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now