fill unsigned char array with hex values

This is a discussion on fill unsigned char array with hex values within the C Programming forums, part of the General Programming Boards category; im trying to fill an unsigned char array with hex values (0x12 0x23 etc) read in from a text file, ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    fill unsigned char array with hex values

    im trying to fill an unsigned char array with hex values (0x12 0x23 etc)
    read in from a text file, i have searched this forum, but not found exactly what i need and my limited knowledge prevents me from adapting the code snippets i found to my needs. the code snippet i have is as follows, i am not posting the whole program because previously i have been flamed for doing that.

    Code:
     FILE *file;
        U8 instructions;
        U8 hexbyte = 0x00;
        U8 hexnibh = 0x00;
        U8 hexnibl = 0x00;
        U8 memory[10] = {0x00};
        U8 progmemory[10] = {0x00};
        U8 convert = 0x00;
        U8 i = 0x00;
           
    
        file = fopen("f:\\L_Endian.txt", "r");
    
        while(! feof(file)){
        fscanf(file, "%c", &memory[instructions++]);
            hexnibh=memory[convert++];
            hexnibh =(hexnibh - '0');
            hexnibh = (hexnibh << 4);
    
        fscanf(file, "%c", &memory[instructions++]);
            hexnibl=memory[convert++];
            hexnibl =(hexnibl - '0');
            hexbyte = (hexnibh ^ hexnibl);
            progmemory[i++] = hexbyte;
        }
        fclose(file);

    what i want to do is read in a file with numbers like 1234567890...
    convert to hex values
    into an array[0x12]
    array[0x34]
    array[0x56]
    array[0x78]
    array[0x90]

    each element being an unsigned char type
    this code nearly works except i get extra (unwanted)byte 0x0A and in the array

    if thas has been done allready can you point me to the location please

    luigi

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > U8 instructions;
    You don't initialise it to 0 before using it to populate your array

    > while(! feof(file))
    Don't do this - see the FAQ
    feof() is an indication of the past (have I just gone past end of file), not a prediction (am I at the end yet)

    > this code nearly works except i get extra (unwanted)byte 0x0A and in the array
    Well you're using %c to read each character from the file, and this includes all the newlines.
    Add an if() to compare for '\n' characters and react appropriately.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21