#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    14
    Rep Power
    0

    Read hex contents from file and store in unsigned char array?


    I have a txt file, content of file is :

    0x1a,0x2b,0xff,0x99,0x55

    I need to store all this value into :

    unsigned char tempArray[10]={0xaa,0xbb,0xcc,0xdd,0xee,0x00,0x00,0x00,0x00,0x00}.

    is there any solution so that my array:

    tempArray[]= {0xaa,0xbb,0xcc,0xdd,0xee,0x1a,0x2b,0xff,0x99,0x55}.

    need to store values from .text file into specific location of array USING C language?

    Thanking you in anticipation of favorable replies.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    14
    Rep Power
    0
    i have tried from my previous thread solution but not works on integer values...
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4300
    Honestly, why did you not use the %x format specifier like a reply on the previous thread indicated. Don't use getc, use fscanf() and the %x specifier and everything should just work:
    Code:
    #include <stdio.h>
    
    int main(void) {
        unsigned char tmparray[10] = {0xaa,0xbb,0xcc,0xdd,0xee,0x00,0x00,0x00,0x00,0x00};
    
        int idx = 5;
        int data;
        FILE *fp = fopen("1.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "Can't read 1.txt");
            return 0;
        }
        while (fscanf(fp, "%*c%*c%x,", &data) == 1) {
            tmparray[idx++] = (unsigned char)data;
        }
        fclose(fp);
    
        for (idx = 0; idx < sizeof(tmparray) / sizeof(tmparray[0]); idx++) {
            printf("%#x,", tmparray[idx]);
        }
        printf("\n");
    
        return 0;
    }
    and when I run it:
    Code:
    WL-MB-MAC:cpp mb$ cat 1.txt 
    0x1a,0x2b,0xff,0x99,0x55
    WL-MB-MAC:cpp mb$ ./sachinmm 
    0xaa,0xbb,0xcc,0xdd,0xee,0x1a,0x2b,0xff,0x99,0x55,
    Enjoy!

    Comments on this post

    • Will-O-The-Wisp agrees : Thank you!
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    14
    Rep Power
    0
    thanks for reply if i do same code for below my txt file data is 498 now and same copy into array from position [8] to end but it give me following error ==>

    Code:
    output with error ==>
    sachin@sachin:~/Pictures$ ./a.out 
    0x5a,0xa5,0xb,0x1,0xf4,0x1,0,0,0xa5,0x5a,0xa,0x1,0xf6,0x1,0,0,0x8,0,0x4,0x2b,0x5e,0,0x39,0xdb,0xe6,0xdf,0x42,0xd9,0xbb,0xef,0x41,0xab,0xe4,0x64,0x42,0xca,0xbf,0xe8,0x29,0x84,0xf7,0xe4,0xc4,0xb8,0x81,0xd8,0x79,0x1c,0xf9,0xee,0x83,0xb8,0xc7,0xd0,0xa1,0x74,0xf7,0xe7,0x46,0xb9,0x85,0xd8,0xe9,0xfc,0xf7,0xef,0xc4,0xb8,0x83,0xe0,0xe5,0xdc,0xe4,0xdb,0xc2,0xf7,0x41,0xef,0x45,0x25,0xe5,0xdd,0x2,0xa9,0x43,0xe7,0x59,0x6d,0xe5,0x61,0x3,0x28,0x7a,0xf,0x65,0x3d,0x97,0xef,0x46,0x99,0xc5,0xd0,0xcd,0x45,0xe6,0xe9,0x6,0x99,0x85,0xde,0x39,0x72,0xe6,0x6b,0xc2,0xb8,0xb9,0xe0,0x89,0xba,0xb8,0xdc,0x4,0xd8,0x7f,0xd8,0xe1,0x73,0xe9,0xe7,0xc5,0xd9,0x87,0xe0,0x29,0x76,0xe8,0x14,0x6,0x78,0xc7,0xe9,0x49,0x32,0xe5,0x6c,0x44,0xc8,0xbb,0xe0,0x4d,0x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21,0x35,0x43,0x63,0x54,0x8f,0x39,0x25,0x63,0x28,0x44,0x5f,0x53,0x17,0x53,0x25,0x27,0x37,0x72,0x52,0x32,0x46,0x62,0xf9,0xf,0xa1,0x90,0x32,0x76,0x91,0x53,0x14,0xff,0x7f,0x5a,0xf2,0x6f,0x54,0x78,0xfa,0x26,0x56,0x33,0xb6,0x42,0x75,0x5b,0x59,0x56,0x24,0x63,0x33,0x5a,0x35,0xff,0xff,0x63,0xf1,0xff,0xff,0xff,0xf2,0x9f,0xff,0x4f,0x83,0x55,0x2f,0x3f,0x6f,0x16,0x21,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    *** stack smashing detected ***: ./a.out terminated
    ======= Backtrace: =========
    /lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb76fbeb5]
    /lib/i386-linux-gnu/libc.so.6(+0x104e6a)[0xb76fbe6a]
    ./a.out[0x8048707]
    /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb76104d3]
    ./a.out[0x80484f1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:03 4981138    /home/sachin/Pictures/a.out
    08049000-0804a000 r--p 00000000 08:03 4981138    /home/sachin/Pictures/a.out
    0804a000-0804b000 rw-p 00001000 08:03 4981138    /home/sachin/Pictures/a.out
    09393000-093b4000 rw-p 00000000 00:00 0          [heap]
    b75c2000-b75de000 r-xp 00000000 08:01 2753533    /lib/i386-linux-gnu/libgcc_s.so.1
    b75de000-b75df000 r--p 0001b000 08:01 2753533    /lib/i386-linux-gnu/libgcc_s.so.1
    b75df000-b75e0000 rw-p 0001c000 08:01 2753533    /lib/i386-linux-gnu/libgcc_s.so.1
    b75f6000-b75f7000 rw-p 00000000 00:00 0 
    b75f7000-b779b000 r-xp 00000000 08:01 2753512    /lib/i386-linux-gnu/libc-2.15.so
    b779b000-b779d000 r--p 001a4000 08:01 2753512    /lib/i386-linux-gnu/libc-2.15.so
    b779d000-b779e000 rw-p 001a6000 08:01 2753512    /lib/i386-linux-gnu/libc-2.15.so
    b779e000-b77a1000 rw-p 00000000 00:00 0 
    b77b5000-b77b9000 rw-p 00000000 00:00 0 
    b77b9000-b77ba000 r-xp 00000000 00:00 0          [vdso]
    b77ba000-b77da000 r-xp 00000000 08:01 2753492    /lib/i386-linux-gnu/ld-2.15.so
    b77da000-b77db000 r--p 0001f000 08:01 2753492    /lib/i386-linux-gnu/ld-2.15.so
    b77db000-b77dc000 rw-p 00020000 08:01 2753492    /lib/i386-linux-gnu/ld-2.15.so
    bfed6000-bfef7000 rw-p 00000000 00:00 0          [stack]
    Aborted (core dumped)

    Code:
    #include <stdio.h>
    int main(void) {
    unsigned char tmparray[506] = 
    {0x5A,0xA5,0x0B,0x01,0xF4,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    
        int idx = 8;
        int data;
        FILE *fp = fopen("8.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "Can't read 1.txt");
            return 0;
        }
        while (fscanf(fp, "%*c%*c%x,", &data) == 1) {
            tmparray[idx++] = (unsigned char)data;
        }
        fclose(fp);
    
        for (idx = 0; idx < sizeof(tmparray) / sizeof(tmparray[0]); idx++)
        {
            printf("%#x,", tmparray[idx]);
        }
        printf("\n");
    
        return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    14
    Rep Power
    0
    this time my text file data is ==>
    0xA5,0x5A,0x0A,0x01,0xF6,0x01,0x00,0x00,0x08,0x00,0x04,0x2B,0x5E,0x00,0x39,0xDB,0xE6,0xDF,0x42,0xD9, 0xBB,0xEF,0x41,0xAB,0xE4,0x64,0x42,0xCA,0xBF,0xE8,0x29,0x84,0xF7,0xE4,0xC4,0xB8,0x81,0xD8,0x79,0x1C, 0xF9,0xEE,0x83,0xB8,0xC7,0xD0,0xA1,0x74,0xF7,0xE7,0x46,0xB9,0x85,0xD8,0xE9,0xFC,0xF7,0xEF,0xC4,0xB8, 0x83,0xE0,0xE5,0x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FF,0xE5,0x0E,0x49,0x38,0x4C,0x59,0x7F,0xD8,0xE9,0xCE,0x46,0x6B,0xE0,0xC6, 0x2C,0xFF,0xE5,0x06,0x48,0x3C,0x4C,0x39,0xFF,0xD1,0xF6,0xD6,0x45,0xCF,0x87,0xF6,0xCF,0x07,0x62,0x09, 0x49,0x43,0x10,0xFC,0x8D,0xF9,0x66,0x31,0x48,0x5D,0x83,0xF7,0xC5,0xFF,0x99,0x39,0x45,0x77,0x85,0xA9, 0xF3,0xD7,0xBD,0x21,0x44,0xEF,0x08,0xF8,0xF9,0xFE,0xC5,0x69,0x45,0x70,0x47,0xC8,0xFB,0xE7,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x94,0x17,0x6A,0xBB,0xF4,0x14,0x3F,0xD6,0xB3,0x44,0x21,0x35,0x43, 0x63,0x54,0x8F,0x39,0x25,0x63,0x28,0x44,0x5F,0x53,0x17,0x53,0x25,0x27,0x37,0x72,0x52,0x32,0x46,0x62, 0xF9,0x0F,0xA1,0x90,0x32,0x76,0x91,0x53,0x14,0xFF,0x7F,0x5A,0xF2,0x6F,0x54,0x78,0xFA,0x26,0x56,0x33, 0xB6,0x42,0x75,0x5B,0x59,0x56,0x24,0x63,0x33,0x5A,0x35,0xFF,0xFF,0x63,0xF1,0xFF,0xFF,0xFF,0xF2,0x9F, 0xFF,0x4F,0x83,0x55,0x2F,0x3F,0x6F,0x16,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x5E,0xE6,
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4300
    Is the data in your input text file all on one line, or does it have multiple lines and spaces like your post above does. If it is all one line, it works perfectly.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    14
    Rep Power
    0

    Thumbs up


    yes all the data in text file are in one line only...i will try thanks once again for your reply

IMN logo majestic logo threadwatch logo seochat tools logo