Thread: Looking to extract a hex value out of a buffer

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    Looking to extract a hex value out of a buffer

    I did received help on how to display a hex value.
    I'm now looking for a way to extract a hex value out of a buffer.

    Ex.

    I have a buffer( 200 bytes long).
    The first 38 bytes contains rt, account and serial number as readable numerics.

    0012333990000000080002340000051751177
    From this I'm able to manipulate via string copies ....

    strncpy(&rt,buffer_input + 1,10);
    strncpy(&account,buffer_input + 11,18); ..ect

    Postions 39-46 is the amount field *******IN HEX *******.
    00000050
    0000000C

    QUESTION 1.....how can I display the hex value from this buffer.
    (can I use a strncpy + other manipulations)
    strncpy(&hex,buffer_input + 39,8);

    QUESTION 2 can I change this hex value to a "readable" numeric and write out to a file.

    Thanks for any recommendations in advance.

    MMC

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try looking into sprintf().

    sprintf() works similar to printf except that it prints to an area of memory instead of the screen. %x is hex format specifier i think.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    Thanks Stoned_coder

    But what I think I'm looking for is

    Positions/bytes in my buffer
    1234.............................................. .................38

    0012333990000000080002340000051751177

    39.........47

    00000050
    0000000C

    How do I get pos 39 - 47 in its own buffer/field to display +
    how do I turn it into a value that I can add to other numbers.

    Any thoughts ??????

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Assuming the data isn't ascii, you could do this:

    int num;
    num = *((int *) buffer_input+38);
    printf("num:%X\n",num);
    printf("num:%d\n",num);

    Assuming an int is 4 bytes, this would take the next 4 bytes and store it in num.

    If it's ascii, then you could to this:

    int num;
    char a[9];

    strncpy(a,buffer_input+38,8);
    *(a+8) = '\0';
    num = atoi(a);
    printf("num:%X\n",num);
    printf("num:%d\n",num);

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    Thanks Swoopy !
    I'll give that a try and post my results.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    Swoopy, I tried and suggestions and I'm now able to display hex values............ at a certain positions.

    BUT....I still not able to set my position correctly.

    Ex.
    int num;
    num = *((int *) buffer_input+38); ....goes to position 133 not 39.
    printf("num:%X\n",num);
    printf("num:%d\n",num);

    Results
    This actually get 4 bytes at position 133.
    4(I guess because I specify int) x 38 + 1 = 133

    Other tests

    num = *((int *) buffer_input);
    Postion 1
    buffer_input +1 = position 5
    buffer_input + 2 = position 9 ....

    I'm looking to start my 4 bytes at position 39.....any thoughts on how to set my pointer

    if I do buffer_input + 9 = position 37......just now sure how to get to position 39.

    Any thoughts....again thanks for your patience and help.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    MMC,
    How is buffer_input declared? Is it:
    char *buffer_input;

    If yes, then this should do the trick:
    num = *((int *) (buffer_input+38));

    This will move the pointer 38 as a character pointer, before casting to an integer pointer.
    As you have pointed out, as an integer pointer, it moves it 38*4.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This might be a problem with precedence. Have you tried...
    Code:
    int num; 
    num = *((int *) ((char *) buffer_input+38));
    printf("num:%X\n",num); 
    printf("num:%d\n",num);
    You can omit the (char *) if buffer_input is already a char buffer.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    I'm able to use
    num = *((char*) buffer_input+44);
    printf("num:%X\n",num);
    printf("num:%d\n",num);

    I used char instead of int ...
    This gives me the hex and integer value for a position.

    Is this my only option ?

    Anyway, thanks to all....I feel I'm making progress !!!

    MMC

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    11

    Thumbs up

    Thanks Everyone !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  5. How to extract a hex value from a string?
    By dlaw in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2002, 02:11 PM