Why am I getting the wrong output? I have a feeling it has something to do with using type char vs. int....but im not specifically too sure. Please help!

1.
      blade71(130)% gcc -o vt visitype.c
   2.
      blade71(131)% vt < vt.in > vt.out
   3.
      blade71(132)% cat vt.out
   4.
      The code 55 represents U
   5.
      The code 4c represents L
   6.
      The code 56 represents V
   7.
      The code 53 represents S
   8.
      The code 41 represents A
   9.
      The code 20 represents SP
  10.
      The code 4e represents N
  11.
      The code 53 represents S
  12.
      The code 45 represents E
  13.
      The code 0 represents NUL
  14.
      The code 4e represents N
  15.
      The code 58 represents X
  16.
      blade71(133)% cat vt.in
  17.
      }~a,bzZ
  18.
      blade71(134)% od -x vt.in
  19.
      0000000 0102 7d7e 612c 627a 5c7f 5a0a
  20.
      0000014
  21.
      blade71(135)% cat visitype.c
  22.
      #include <stdio.h>
  23.
      #define MAX 1000
  24.
       
  25.
      void aVal(char asciiname[], char S[]);
  26.
       
  27.
      main ()
  28.
      {
  29.
      int i, c, j;
  30.
      char asciiname[] =
  31.
      "NUL\0" "SOH\0" "STX\0" "ETX\0" "EOT\0" "ENQ\0" "ACK\0" "BEL\0"
  32.
      " BS\0" " HT\0" " NL\0" " VT\0" " NP\0" " CR\0" " SO\0" " SI\0"
  33.
      "DLE\0" "DC1\0" "DC2\0" "DC3\0" "DC4\0" "NAK\0" "SYN\0" "ETB\0"
  34.
      "CAN\0" " EM\0" "SUB\0" "ESC\0" " FS\0" " GS\0" " RS\0" " VS\0"
  35.
      " SP\0" " !\0" " \"\0" " #\0" " $\0" " %\0" " &\0" " '\0"
  36.
      " (\0" " )\0" " *\0" " +\0" " ,\0" " -\0" " .\0" " /\0"
  37.
      " 0\0" " 1\0" " 2\0" " 3\0" " 4\0" " 5\0" " 6\0" " 7\0"
  38.
      " 8\0" " 9\0" " :\0" " ;\0" " <\0" " =\0" " >\0" " ?\0"
  39.
      " @\0" " A\0" " B\0" " C\0" " D\0" " E\0" " F\0" " G\0"
  40.
      " H\0" " I\0" " J\0" " K\0" " L\0" " M\0" " N\0" " O\0"
  41.
      " P\0" " Q\0" " R\0" " S\0" " T\0" " U\0" " V\0" " W\0"
  42.
      " X\0" " Y\0" " Z\0" " [\0" " \\\0" " ]\0" " ^\0" " _\0"
  43.
      " `\0" " a\0" " b\0" " c\0" " d\0" " e\0" " f\0" " g\0"
  44.
      " h\0" " i\0" " j\0" " k\0" " l\0" " m\0" " n\0" " o\0"
  45.
      " p\0" " q\0" " r\0" " s\0" " t\0" " u\0" " v\0" " w\0"
  46.
      " x\0" " y\0" " z\0" " {\0" " |\0" " }\0" " ~\0" "DEL\0"
  47.
      ;
  48.
      char S[MAX], *p1;
  49.
      p1 = S;
  50.
      c = getchar();
  51.
      for (i = 0; c!=EOF; c=getchar())
  52.
      {
  53.
      S[i] = c;
  54.
      i++;
  55.
      }
  56.
      S[i] = '\0';
  57.
      aVal (asciiname, S);
  58.
      }
  59.
      void aVal(char asciiname[], char S[])
  60.
      {
  61.
      int i, j;
  62.
      int k;
  63.
      i = 0;
  64.
      k = S[i];
  65.
      for ( i = 0; k!='\0'; k = S[i])
  66.
      {
  67.
      j = asciiname[k];
  68.
      printf("The code %3x represents %s\n", j, &asciiname[4*j]);
  69.
      i++;
  70.
      }
  71.
      S[i] = '\0';
  72.
       
  73.
      }

Edited 7 Years Ago by Ineedhelpplz: n/a

The problem is char = 1 byte; int = 4 (sometimes 8 depends on system) bytes. The assignment:

k=s

is either ambiguous or just incorrect. I'm not sure which....How do I either change it to K=S + in order to have them be converted to int type? Do I need another function to convert the program or is there some kind of typecasts I can use?

My error test displays the correct bits in S[] however, because it is type char and not int they spill over into the next element...producing the wrong answer.

Err Test 2: 1
Err Test 2: 2
Err Test 2: 7d // Recall this from the octal dump
Err Test 2: 7e // done earlier...it is the correct value
Err Test 2: 61
Err Test 2: 2c
Err Test 2: 62
Err Test 2: 7a
Err Test 2: 5c
Err Test 2: 7f
Err Test 2: 5a
Err Test 2: a
The code 55 represents U
The code 56 represents V
The code 53 represents S
The code 41 represents A
The code 20 represents SP
The code 4e represents N
The code 53 represents S
The code 45 represents E
The code 0 represents NUL
The code 4e represents N
The code 58 represents X
The code 4e represents N
The code 0 represents NUL
The code 0 represents NUL
The code 0 represents NUL

Edited 7 Years Ago by Ineedhelpplz: n/a

Updated: Problem solved...however in C it appears logical to derive; when you solve a problem usually you are solving it with another problem. Alas my program works as intended. However, now it is somehow reading 3 ASCII characters that do not exist in the vt.in file. Check out my execution and octal dumps.

Script started on Wed Sep 30 10:19:21 2009
blade71(1)% gcc -o vt visitype.c
blade71(2)% vt<vt.in>vt.out
blade71(3)% cat vt.in
}~a,bzZ
blade71(4)% cat vt.out
The code 1 represents SOH
The code 2 represents STX
The code 7d represents }
The code 7e represents ~
The code 61 represents a
The code 2c represents ,
The code 62 represents b
The code 7a represents z
The code 5c represents \
The code 7f represents DEL
The code 5a represents Z
The code a represents NL
The code 0 represents NUL
The code 3b represents ;
The code 73 represents s
The code ffffff80 represents
blade71(5)% where is the extra 3b, 73, and value ffffff80 coming from?
blade71(6)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(7)% od -x vt.out
0000000 5468 6520 636f 6465 2020 2031 2072 6570
0000020 7265 7365 6e74 7320 534f 480a 5468 6520
0000040 636f 6465 2020 2032 2072 6570 7265 7365
0000060 6e74 7320 5354 580a 5468 6520 636f 6465
0000100 2020 3764 2072 6570 7265 7365 6e74 7320
0000120 2020 7d0a 5468 6520 636f 6465 2020 3765
0000140 2072 6570 7265 7365 6e74 7320 2020 7e0a
0000160 5468 6520 636f 6465 2020 3631 2072 6570
0000200 7265 7365 6e74 7320 2020 610a 5468 6520
0000220 636f 6465 2020 3263 2072 6570 7265 7365
0000240 6e74 7320 2020 2c0a 5468 6520 636f 6465
0000260 2020 3632 2072 6570 7265 7365 6e74 7320
0000300 2020 620a 5468 6520 636f 6465 2020 3761
0000320 2072 6570 7265 7365 6e74 7320 2020 7a0a
0000340 5468 6520 636f 6465 2020 3563 2072 6570
0000360 7265 7365 6e74 7320 2020 5c0a 5468 6520
0000400 636f 6465 2020 3766 2072 6570 7265 7365
0000420 6e74 7320 4445 4c0a 5468 6520 636f 6465
0000440 2020 3561 2072 6570 7265 7365 6e74 7320
0000460 2020 5a0a 5468 6520 636f 6465 2020 2061
0000500 2072 6570 7265 7365 6e74 7320 204e 4c0a
0000520 5468 6520 636f 6465 2020 2030 2072 6570
0000540 7265 7365 6e74 7320 4e55 4c0a 5468 6520
0000560 636f 6465 2020 3362 2072 6570 7265 7365
0000600 6e74 7320 2020 3b0a 5468 6520 636f 6465
0000620 2020 3733 2072 6570 7265 7365 6e74 7320
0000640 2020 730a 5468 6520 636f 6465 2066 6666
0000660 6666 6638 3020 7265 7072 6573 656e 7473
0000700 200a
0000702
blade71(9)% exit

script done on Wed Sep 30 10:21:42 2009

blade71(2)% cat visitype.c
#include <stdio.h>
#define MAX 1000

void aVal(char asciiname[], char S[]);

main ()
{
int i, c, j;
char asciiname[] =
        "NUL\0"  "SOH\0"  "STX\0"  "ETX\0"  "EOT\0"  "ENQ\0"  "ACK\0"  "BEL\0"
        " BS\0"  " HT\0"  " NL\0"  " VT\0"  " NP\0"  " CR\0"  " SO\0"  " SI\0"
        "DLE\0"  "DC1\0"  "DC2\0"  "DC3\0"  "DC4\0"  "NAK\0"  "SYN\0"  "ETB\0"
        "CAN\0"  " EM\0"  "SUB\0"  "ESC\0"  " FS\0"  " GS\0"  " RS\0"  " VS\0"
        " SP\0"  "  !\0"  "  \"\0" "  #\0"  "  $\0"  "  %\0"  "  &\0"  "  '\0"
        "  (\0"  "  )\0"  "  *\0"  "  +\0"  "  ,\0"  "  -\0"  "  .\0"  "  /\0"
        "  0\0"  "  1\0"  "  2\0"  "  3\0"  "  4\0"  "  5\0"  "  6\0"  "  7\0"
        "  8\0"  "  9\0"  "  :\0"  "  ;\0"  "  <\0"  "  =\0"  "  >\0"  "  ?\0"
        "  @\0"  "  A\0"  "  B\0"  "  C\0"  "  D\0"  "  E\0"  "  F\0"  "  G\0"
        "  H\0"  "  I\0"  "  J\0"  "  K\0"  "  L\0"  "  M\0"  "  N\0"  "  O\0"
        "  P\0"  "  Q\0"  "  R\0"  "  S\0"  "  T\0"  "  U\0"  "  V\0"  "  W\0"
        "  X\0"  "  Y\0"  "  Z\0"  "  [\0"  "  \\\0" "  ]\0"  "  ^\0"  "  _\0"
        "  `\0"  "  a\0"  "  b\0"  "  c\0"  "  d\0"  "  e\0"  "  f\0"  "  g\0"
        "  h\0"  "  i\0"  "  j\0"  "  k\0"  "  l\0"  "  m\0"  "  n\0"  "  o\0"
        "  p\0"  "  q\0"  "  r\0"  "  s\0"  "  t\0"  "  u\0"  "  v\0"  "  w\0"
        "  x\0"  "  y\0"  "  z\0"  "  {\0"  "  |\0"  "  }\0"  "  ~\0"  "DEL\0"
        ;
        char S[MAX], *p1;
        p1 = S;
        c = getchar();
        for (i = 0; c!=EOF; c=getchar())
        {
                S[i] = c;
                i++;
        }
        S[i] = '\0';
        aVal (asciiname, S);
}
        void aVal(char asciiname[], char S[])
        {
                int i;
                char k, j;
                i = 0;
                k = S[i];
                for ( i = 0; k!=EOF; k = S[i])
                {
                k = S[i];
                j = k;
                printf("The code %3x represents %s\n", j, &asciiname[4*j]);
                        i++;
                }

        }
This question has already been answered. Start a new discussion instead.