Thread: Converting char array to int16 type

  1. #1
    Registered User
    Join Date
    Mar 2013

    Cool Converting char array to int16 type

    Hello guys,

    i know this is a very old question with millions of solutions and believe me i have tried some of those solutions but the result is not coming up.

    I am trying to convert a char buffer[1024] with raw data to int 16 buffer2[1024] which is supposed to have hex data (0x01, 0x02 kind of format).

    code i am trying with is:
         char temp_buffer[1024];
         block_size = sizeof(temp_buffer);
         num_blocks = sizeof(char);
         Status =  mount(0, &myfsObject);
         if(Status != DAVEApp_SUCCESS)
         ptr = fopen("test1.raw", "r+");
         fseek(ptr, 0, SEEK_SET);
         result = fread(&temp_buffer, num_blocks, block_size, ptr);
    for (i=0;i<1024;i++)
            UTXBuf1[i] =  temp_buffer[i] - '0';
           ptr = fopen("test2.txt", "w+");
                  fseek(ptr, 0, SEEK_SET);
                  block_size = sizeof(UTXBuf1);
                  //num_blocks = sizeof(char);
                  fwrite(&UTXBuf1, 1, block_size, ptr);
                  status1 = fclose(ptr);
    As you can figure out that i am verifying the data of resultant buffer by creating a new file and writing data to it.

    The result which i got is:
    H    ья  H   ья  H    ья  H    ья  H    ья  H   ья  H   ья  H    ья
    If something is missing then please let me know.

    Any help would be appreciated.


  2. #2
    Registered User
    Join Date
    May 2012
    When people talk about arrays holding "hex data", you can be pretty sure they've misunderstood.
    Numbers can be held as binary data, raw ints, on which operations like + and * work as expected, or as ascii (or unicode) representations. An ascii representation of a number could be denary, hex, or occasionally another representation, e.g. Roman numerals. It is a string. It must be treated as text, and you cannot do arithmetical operations with it.

    Because hex and decimal are so common, functions like printf(), scanf() and atoi() have special support for them. Also, C source code itself is human-reable ascii. So of course when we type int x = 42; the 42 is held, in the source file, in an ascii denary representation. But it is turned into binary by the compiler, and the string "42" will not appear in the machine code.

    Probably what you want to do is to convert chars (normally 8 bits) to 16 bit integers, which can be done by a simple assignment loop. Then you want to print them out to a file using fprintf(fp, "0x%02x"). At the moment it looks like you're reading a binary file as text, which will just give you lots of funny extended ascii characters.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.

  3. #3
    Registered User
    Join Date
    Mar 2013
    thanks for your prompt response.

    i do not really want to print the output in a file, it's just for the verification purpose.

    Another thing is, Yes i want to convert the char to int16 because the buffer which will be processed further is int16 type.

    But when i myself give hex inputs to the int16 buffer (with out any external file reading) it works perfect but when i give input by reading a raw/binary file it gives nothing

    Anyways, i would like to share the though of "simple assignment loop" you have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting int stored in array to char?
    By Meerul264 in forum C++ Programming
    Replies: 1
    Last Post: 12-24-2012, 08:31 AM
  2. Converting string to char array
    By frankchester in forum C Programming
    Replies: 13
    Last Post: 12-01-2010, 01:33 AM
  3. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 09:51 PM
  5. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM