C Programming problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mjolinor
    V.I.P. VIC
    • Jan 2009
    • 1093

    #1

    C Programming problem

    Needing to write a small C programme to number crunch (hate C)

    I have these numbers
    aa 00 3d fe cd ff 22 00 3d aa 00 39 fe c4 ff 1f

    aa is my placemarker as it is a serial stream, next 6 bytes are my numbers, 2s compliment so that makes it

    003d fecd ff22

    First one is +ve, second 2 are negative. The next 2 bytes in teh string are a bug in the chip and can be discarded then it starts again.

    I can
    num = fgetc (infile);
    printf ("%d", num);

    and it prints a number between -128 and +128 but

    x = fgetc (infile);
    x = x << 8;
    x = x + fgetc (infile);

    That gives me a number between 0 and 65535 ie always +ve.

    I want to stick these numbers in a file with 3 columns of decimal numbers between -32k and +32k

    What printf format do I need?
  • chroma
    V.I.P. Member
    • Feb 2009
    • 1976

    #2
    so you just want to print columns?
    like:
    Code:
    printf ("Column 1     Column2     Column 3"\n);
    printf ("%6.31f%6.31f%6.31f\n",num1,num2,num3);
    This specifies an output to printf that there will be three columns 6chars wide (enough to spew out 999999) and that they'll be floats with 3 decimal places hence the .31f (000000.000 to 999999.999) then it passes the variables num1~3 to these columns.

    If you where wanting the first a positive and the next two negative then just add in - signage after the modulus like
    Code:
    printf ("Column 1     Column2     Column 3"\n);
    printf ("%6.31f%-6.31f%-6.31f\n",num1,num2,num3);

    I'm not too keen on printf or C in general I know a little but i know way more about C++ and cout/fout.
    But take it your doing serial port work and it needs to be C?
    He who laughs last thinks slowest.

    Comment

    • Mjolinor
      V.I.P. VIC
      • Jan 2009
      • 1093

      #3
      Originally posted by chroma
      so you just want to print columns?
      like:
      Code:
      printf ("Column 1     Column2     Column 3"\n);
      printf ("%6.31f%6.31f%6.31f\n",num1,num2,num3);
      This specifies an output to printf that there will be three columns 6chars wide (enough to spew out 999999) and that they'll be floats with 3 decimal places hence the .31f (000000.000 to 999999.999) then it passes the variables num1~3 to these columns.

      If you where wanting the first a positive and the next two negative then just add in - signage after the modulus like
      Code:
      printf ("Column 1     Column2     Column 3"\n);
      printf ("%6.31f%-6.31f%-6.31f\n",num1,num2,num3);

      I'm not too keen on printf or C in general I know a little but i know way more about C++ and cout/fout.
      But take it your doing serial port work and it needs to be C?
      Not bothered about columns and all that, what I need is to take a signed 16 bit hex number and print it. There does not seem to be a format for it, once I shift the MSB left 8 then it behaves as if it is unsigned so when I add the LSB I end up with an unsigned 16 bit number. I got round it by shifting, adding, testing the MS bit, inverting and adding one if the MS bit was set then attaching a sign to it.

      There does not seem to be a simpler way to do it than with a page of code, typical C really

      It would have been easy if they had been 8 bit numbers, printf would have handled it but it seems not with 16 bit or higher.

      Comment

      • chroma
        V.I.P. Member
        • Feb 2009
        • 1976

        #4
        ah im with you now.
        It does seem strange, there's likely to be some obscure header to deal with it, like i said i merely dabble with C and thats only to work around problems in C++ to be honest

        Your not by any chance just using plain old vanilla int as your type are you? if so switch to signed long instead, this should remove the 8bit signed ceiling (under some compilers it might not be parsed unless its signed int16) if you need more space then set it up as signed int32
        The signed prefix isnt always necessary but some compilers default to unsigned unless prefixed, some default to signed unless uint16 or uint32 is defined... id stick to prefixing

        Post up your code and let me take a look.
        He who laughs last thinks slowest.

        Comment

        • Mjolinor
          V.I.P. VIC
          • Jan 2009
          • 1093

          #5
          Originally posted by chroma
          ah im with you now.
          It does seem strange, there's likely to be some obscure header to deal with it, like i said i merely dabble with C and thats only to work around problems in C++ to be honest

          Your not by any chance just using plain old vanilla int as your type are you? if so switch to signed long instead, this should remove the 8bit signed ceiling (under some compilers it might not be parsed unless its signed int16) if you need more space then set it up as signed int32
          The signed prefix isnt always necessary but some compilers default to unsigned unless prefixed, some default to signed unless uint16 or uint32 is defined... id stick to prefixing

          Post up your code and let me take a look.

          Tried every combination of signed unsigned long short double float etc etc ended up bit bashing it

          It works fine really , I just didn't want to bother putting in the work as I thought it should be available without. Now it's working I can just leave it.

          Comment

          Working...