Thread: fill unsigned char array with hex values

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

    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.

     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 =(hexnibh - '0');
            hexnibh = (hexnibh << 4);
        fscanf(file, "%c", &memory[instructions++]);
            hexnibl =(hexnibl - '0');
            hexbyte = (hexnibh ^ hexnibl);
            progmemory[i++] = hexbyte;

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

    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


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

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, 06:03 PM