Converting RAW data to ASCII Signed Integers 16-bit

This is a discussion on Converting RAW data to ASCII Signed Integers 16-bit within the C++ Programming forums, part of the General Programming Boards category; This is not a school assignment. I have some RAW files that I need to convert into ASCII data. 16-bit ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Converting RAW data to ASCII Signed Integers 16-bit

    This is not a school assignment.

    I have some RAW files that I need to convert into ASCII data. 16-bit signed integers. Here is the result I am looking for

    Code:
    const TESTDATA[] ATTRIBUTE_ALIGN( 32 ) =
    {
             0,      0,    219,     61,   -289,   -491,   -475,   -367,
          -549,  -1115,  -1087,   -857,  -1183,   -712,    373,   1147,
          2733,   4312,   3440,   1111,   -513,  -1840,  -2080,   -139,
           943,   -227,  -1105,  -1045,  -1185,  -1391,   -984,   -549,
          -685,   -803,  -1072,  -1817,  -2131,  -2144,  -2821,  -3931,
         -5181,  -6601,  -7507,  -7068,  -5316,  -2864,   -797,   -296,
          -488,   -123,      8,  -1164,  -2801,  -2881,  -2437,  -3100,
         -2997,  -1700,   -783,   -155,    408,    623,    779,    224,
         -1977,  -4167,  -4763,  -4504,  -3864,  -3199,  -2891,  -2051,
           119,   2379,   3829,   6088,   8829,  10232,  11181,  11582,
         10096,   7707,   5892,   3763,    216,  -2027,   -641,   1795,
          3753,   5995,   7212,   6048,   4051,   3623,   4205,   4709,
          6556,   9495,  11444,  12430,  12889,  12228,  10732,   9289,
    }
    Above, the result I seek (as written to afile)

    And the code I have is:

    Code:
    #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
    
    #include "stdafx.h"
    
    int main(int argc, char* argv[])
    {
    FILE *fp,*of;
    int num;
    char counter=0,a,b;
    
    	if (argc != 4 && argc != 5) {
    		puts("Usage: CONVERTER inputfile.RAW output.c structurename [flip]");
    		return 0;
    		}
    
    	fp=fopen((const char *)argv[1],(const char *)"rb");
    	if (fp) {
    		of=fopen((const char *)argv[2],(const char *)"w");
    		if (of) {
    			fprintf(of,"\n#include \"TEST.h\"\n\nconst s16 %s[] ATTRIBUTE_ALIGN( 32 ) =\n{\n",argv[3]);
    
    			do {
    				a=0;
    				b=0;
    				if (!(feof(fp))) a=fgetc(fp);
    				if (!(feof(fp))) b=fgetc(fp);
    				num=0;
    				if (argc==4) {	//don't flip
    					num=(a*256+b);
    					if (num > 32767) num=(num-32768)*-1;
    					}
    				else {
    					num=(b*256+a);
    					if (num > 32767) num=(num-32768)*-1;
    					}
    				if (counter != 0) fprintf(of,", %7d",num);
    				else fprintf(of,"     %7d",num);
    				counter++;
    				if (counter==8) {
    					if (!(feof(fp))) fprintf(of,",\n");
    					else fprintf(of,"\n");
    					counter=0;
    					}
    				} while (!(feof(fp)));
    			if (counter != 0) fprintf(of,"\n");
    			fprintf(of,"\n};");
    			fclose(of);
    			}
    		else {
    			puts("Error: could not open outputfile");
    			}
    		fclose(fp);
    		}
    	else {
    		puts("Error: could not open inputfile");
    	}
    	return 0;
    }
    Where did I go WRONG?? the converter converts data OK, but the result exceeds 32768, the limit ??

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > I have some RAW files that I need to convert into ASCII data. 16-bit signed integers.

    You do know that ASCII is a 7-bit encoding, don't you?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    The RAW data is actually .wav files where the headers etc. have been stripped out. all thats left in the raw data is the actual audio. 16-bit PCM .wav files. did you spot an obvious dumb mistake?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'm still trying to understand what it is you want to do. Are you trying to put a 16-bit number into a 7-bit ASCII code? Because there will be some loss of data if you try to do that.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    This is what I am trying to do: http://www.kvi.nl/~nijboer/wavutils/wavutils.htm

    (except in my case, I ignore the .wav header chunks as those are already stripped out of my RAWfiles)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I take it that by "ASCII" you mean "human-readable", not ASCII. The computation that you have doesn't seem like it should overflow, but it won't give you the right answer -- for instance 0xFFFF should represent -1 (add 1 and you get all bits zero), but you have it as a large negative number. You could maybe check that a and b don't ever exceed 255.

    If portability isn't a huge concern, check your cstdint or climits (or something like that) header files to see if you have a int16_t type. (It might be "short" on your system.) Then you could just fread directly into a variable of that type without conversion.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I understand that you want to convert from 16 bit signed to ASCII TEXT.

    I think you need to concern yourself with the sign of the leftmost char when multiplying and disregard the sign of the rightmost char when adding.

    Or, instead of casting into an int, you could use a short, like this:
    Code:
    short value ; 
    unsigned char a=fgetc(fp) ; 
    unsigned char b=fgetc(fp) ; 
    value = a ; 
    value <<= 8 ; 
    value += b ;
    This would just assemble the signed value back together. Something along these lines should work - I didn't test it. (And you need the swap-logic too for dealing with big endian or little endian).

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  2. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  3. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM
  4. 16 bit or 32 bit
    By Juganoo in forum C Programming
    Replies: 9
    Last Post: 12-19-2002, 07:24 AM

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