Thread: How to ...read in a hex value and display

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

    How to ...read in a hex value and display

    I'm trying to read in a hex value and display that value....any thoughts on how I can modify the following code.
    Thanks in advance

    File layout .................................................. ......

    ..í^...........
    005B00000000000
    005000000000000
    --------------
    ..êY...........
    005E00000000000
    002800000000000
    --------------
    ..<............
    004200000000000
    .................................................. ...........................

    Code .................................................. .........................

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <IO.h>
    main(void)
    {
    FILE *input;
    char *buffer_input;
    int hex;
    buffer_input = malloc(50000);
    /************************************************** *****/
    /* open input file */
    /************************************************** *****/
    if (buffer_input < 1)
    printf("buff alloc failed\n");
    if ((input = fopen("DD:IN","rb")) == NULL)
    {
    printf("open of IN DD failed\n");
    exit(-1);
    }
    else
    {
    printf("Successful open\n");
    }
    /************************************************** *****/
    /* perform first read into buffer */
    /************************************************** *****/
    while (!feof(input))
    {
    fread(buffer_input,sizeof(int),80,input);
    strncpy(&hex,buffer_input + 1,8);
    printf("\nhex = %x\n", hex);
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option is to just use an int array.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    //How many to read in one pass
    const int num_of_items = 80;
    int main(void)
    { 
    FILE *input; 
    int *buffer_input;
    int i, items;
    buffer_input = malloc(num_of_items*sizeof(int));
    /*************************************************/
    /* open input file */ 
    /*************************************************/
    if (buffer_input == NULL)
    printf("buff alloc failed\n");
    if ((input = fopen("DD:IN","rb")) == NULL)
    {
    printf("open of IN DD failed\n"); 
    exit(-1); 
    } 
    else 
    { 
    printf("Successful open\n"); 
    } 
    /*************************************************/
    /* perform first read into buffer */ 
    /*************************************************/
    while (items = fread(buffer_input,sizeof(int),num_of_items,input))
    {
       for (i=0; i<items; i++)
          printf("\nhex = %x\n",buffer_input[i]);
    }
    free(buffer_input);
    return 0;
    }
    Last edited by swoopy; 04-10-2002 at 01:04 PM.

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

    Thanks

    Thanks Swoopy

    I was able to display the hex values !

    My next step, I will try to incorporate this logic when reading a file with different data types.

    Example

    pos 1-10 = router
    pos 11-29 = account
    pos 30 - 38 = serial
    pos 39 -45 = amount This will be a hex value

    What I want to be able to do is ...... read 45 bytes into a buffer.
    Then display the rt , account and serial..........then display the hex value via your code.
    Any thoughts on "multiple data type processing" would be greatly appreciated. In the meantime, I have enough information to keep me busy.

    Again Thanks ......

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    11
    Swoopy...I was just speaking with someone concerning my last post.
    I guess what I'm really trying to do is......

    Read ...say the first 200 characters into a buffer.
    Within that buffer, is displayable values via a sting copy(I use string copy to isolate individual fields.

    Also embedded within that buffer is a hex value.
    When I writer the buffer to a file...it stops at the hex value ???

    ****Anyway, can I code to display that hex value thats embedded within a char buffer******.

    I also "eventually" would like to write to a file along with the other data.

    Thanks MMC

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    MMC,
    I believe something close to your original code will work well, i.e. use a character buffer to read in the file. And then, as you said, the strcpy() type functions can isolate the fields.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    const int buffer_size = 200;
    int main(void)
    { 
    FILE *input; 
    char *buffer_input;
    int i, items;
    buffer_input = malloc(buffer_size);
    /*************************************************/
    /* open input file */ 
    /*************************************************/
    if (buffer_input == NULL)
    printf("buff alloc failed\n");
    if ((input = fopen("DD:IN","rb")) == NULL)
    {
    printf("open of IN DD failed\n"); 
    exit(-1); 
    } 
    else 
    { 
    printf("Successful open\n"); 
    } 
    /*************************************************/
    /* perform first read into buffer */ 
    /*************************************************/
    while (items =  fread(buffer_input,1,buffer_size,input))
    {
       for (i=0; i<items; i++)
          printf("\nhex = %x\n",buffer_input[i]);
    }
    free(buffer_input);
    return 0;
    }
    One note on your strncpy().

    strncpy(&hex,buffer_input + 1,8);

    Unless an int is 8 bytes on your machine, this 8 should be changed to the size of an int (or use sizeof(int)).

Popular pages Recent additions subscribe to a feed