New line question (preferred) and ASCII symbols

Hey, I need my ASCII table values to start from 0 and go to 7, then start from 8 and go to 15, then start from 16 and go to 23, then 24 to 31 and so on for part 1 and 128 to 135, 136 to 143 and so on for part 2. However, I don't know how to get it with my written code. Can someone please tell me where I am doing it wrong or what I might have to change? The program I have is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>

using namespace std;

int main()
{
  initscr();
  endwin();

  unsigned char c;
  cout << endl;
  cout << "ASCII codes: Part 1" << endl << endl;

  for(c=0; c<=127; c++)
  {
    printf("%d %c \t", c, c);

    if(c == 0){
      continue;
    }
    else if(c%8 == 0){
      printf("\n");
    }

    if(c == 127){
      break;
    }
  }

  unsigned char e;
  cout << endl << endl;
  cout << "ASCII codes: Part 2" << endl << endl;

  for(e=128; e<=255; e++)
  {
    printf("%d %c \t", e, e);

    if(e == 128) {
      continue;
    }
    else if(e%8 == 0) {
      printf("\n");
    }

    if(e == 255)
      break;
    }
  return 0;
}

Currently I get:
ASCII codes: Part 1

0  	1  	2  	3  	4  	5  	6  	7  	8 	
9 	 	10 
 	11 
            	12 
        14      15  	16  	
17  	18  	19  	20  	21  	22  	23  	24  	
25  	26  	27 	28  	29  	30  	31  	32   	
33 ! 	34 " 	35 # 	36 $ 	37 % 	38 & 	39 ' 	40 ( 	
41 ) 	42 * 	43 + 	44 , 	45 - 	46 . 	47 / 	48 0 	
49 1 	50 2 	51 3 	52 4 	53 5 	54 6 	55 7 	56 8 	
57 9 	58 : 	59 ; 	60 < 	61 = 	62 > 	63 ? 	64 @ 	
65 A 	66 B 	67 C 	68 D 	69 E 	70 F 	71 G 	72 H 	
73 I 	74 J 	75 K 	76 L 	77 M 	78 N 	79 O 	80 P 	
81 Q 	82 R 	83 S 	84 T 	85 U 	86 V 	87 W 	88 X 	
89 Y 	90 Z 	91 [ 	92 \ 	93 ] 	94 ^ 	95 _ 	96 ` 	
97 a 	98 b 	99 c 	100 d 	101 e 	102 f 	103 g 	104 h 	
105 i 	106 j 	107 k 	108 l 	109 m 	110 n 	111 o 	112 p 	
113 q 	114 r 	115 s 	116 t 	117 u 	118 v 	119 w 	120 x 	
121 y 	122 z 	123 { 	124 | 	125 } 	126 ~ 	127  	

ASCII codes: Part 2

128 ? 	129 ? 	130 ? 	131 ? 	132 ? 	133 ? 	134 ? 	135 ? 	136 ? 	
137 ? 	138 ? 	139 ? 	140 ? 	141 ? 	142 ? 	143 ? 	144 ? 	
145 ? 	146 ? 	147 ? 	148 ? 	149 ? 	150 ? 	151 ? 	152 ? 	
153 ? 	154 ? 	155 ? 	156 ? 	157 ? 	158 ? 	159 ? 	160 ? 	
161 ? 	162 ? 	163 ? 	164 ? 	165 ? 	166 ? 	167 ? 	168 ? 	
169 ? 	170 ? 	171 ? 	172 ? 	173 ? 	174 ? 	175 ? 	176 ? 	
177 ? 	178 ? 	179 ? 	180 ? 	181 ? 	182 ? 	183 ? 	184 ? 	
185 ? 	186 ? 	187 ? 	188 ? 	189 ? 	190 ? 	191 ? 	192 ? 	
193 ? 	194 ? 	195 ? 	196 ? 	197 ? 	198 ? 	199 ? 	200 ? 	
201 ? 	202 ? 	203 ? 	204 ? 	205 ? 	206 ? 	207 ? 	208 ? 	
209 ? 	210 ? 	211 ? 	212 ? 	213 ? 	214 ? 	215 ? 	216 ? 	
217 ? 	218 ? 	219 ? 	220 ? 	221 ? 	222 ? 	223 ? 	224 ? 	
225 ? 	226 ? 	227 ? 	228 ? 	229 ? 	230 ? 	231 ? 	232 ? 	
233 ? 	234 ? 	235 ? 	236 ? 	237 ? 	238 ? 	239 ? 	240 ? 	
241 ? 	242 ? 	243 ? 	244 ? 	245 ? 	246 ? 	247 ? 	248 ? 	
249 ? 	250 ? 	251 ? 	252 ? 	253 ? 	254 ? 	255 ?


I am running this program on mac os and am also trying to figure out how to print ASCII symbols. Because Im running c++ (g++ compiler on mac) it apparently does not have ASCII symbols or at least not all of them. I do understand that my only option might be to transfer the code to windows os and try it there. However, if you know how I could print it in mac - please share. Also pardon for long message!
Last edited on
10 and 13 (decimal) are both end of lines. Dos/win actually uses BOTH on every line.
128+ should print in a console; they are mostly "console box drawing" symbols + a few others.

0 is end of string. Can't print this.
A number of symbols from 0 to 20 are not printable at all, like the PC speaker beep (/bel ) symbol, and some are for serial port comms and other old things. (your program should have beeped your speakers!).

It has nothing to do with a mac. The ascii table is standard (that is the s in ascii!) and the same on all systems. It has to do with symbols that are simply not meant to be printed, they are for other purposes.

Are you printing to a window or to a console? Windows sometimes have a layer that can't handle the 128 + symbols. Try printing them to a text file, and opening that in a program that CAN display them.

You are not doing anything wrong. You just have to understand what you are working with here.
Last edited on
Thanks jonnin, I am printing them to a console. I more or less understand what you are saying, however, what you mean by understand what you are working with?
you have to know what to expect here. As in not expecting to see most of the first 20, expecting to see the end of lines, etc.

If the console shows '?' symbols, try a text file and a capable text editor. This is a limit of your console.

Last edited on
Considering my code, is it possible to start values from 0 to 7, then start from 8 and go to 15. That way my code would work. Would anyone help me so I would have 8 symbols in a row for all ASCII symbols. Currently not showing them is not the biggest problem. I will tackle it once i know how to represent 8 symbols in a row. Any suggestions?
ASCII codes: Part 2
128 ? 129 ? 130 ? 131 ? 132 ? 133 ? 134 ? 135 ? 136 ?

ASCII ends at 127, there is no "Part 2". The numbers from 128 to 255 are not ASCII codes.

9 10
11
12

The ASCII codes 0-31 and 127 are not printable. There is nothing to show here.

You can fix both by replacing

printf("%d %c \t", c, c);

with something like

1
2
3
4
    if(isprint(c))
      printf("%3d %c \t", c, c); // 3 to line them up with 3-digit numbers
    else
      printf("%3d ▒ \t", c);

(or pick whatever placeholder you like instead of ▒)

Oh, and as for
to start values from 0 to 7, then start from 8 and go to 15

move
1
2
3
if(c%8 == 0){
  printf("\n");
}

above the printf that prints the character.
Last edited on
128+ are extended ascii and as noted not part of the original codes. These are still standardized to be the same on all machines that support it.

This is what you should see:

http://www.theasciicode.com.ar/

Note the first column, which doesn't have any symbols printed, because there are none.

Note the third column, that is extended ascii which is 90% non English symbols and letters that are used a lot + console box drawing + a few strays.



jonnin wrote:
This is what you should see:
http://www.theasciicode.com.ar/

Whoever made that web page doesn't know what they were talking about. The third column has nothing to do with ASCII. It appears to be IBM CP 850 https://en.wikipedia.org/wiki/Code_page_850 (not even CP 437 which that web page mentions in the text below)
Last edited on
The thing is that I need to print all 255 ASCII symbols in the same manner and for this problem it shows all symbols from 0 to 30 as symbols except for 0 and 8. Thank you for the 8 symbols help. It shows up now. However, now I need to print all 255 ASCII symbols in mac. How could I do it? Thank you very much for help. I'll look forward to your replies!
Last edited on
Also I changed my code, but it goes to infinite loop once it finishes with 2 part. What's wrong? The code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <ncurses.h>

using namespace std;

int main()
{
  initscr();
  endwin();
  unsigned char c, e;
  cout << endl;
  cout << "ASCII codes: Part 1" << endl << endl;

  for(c=0; c<=127; c++)
  {
    if(c%8 == 0){
      printf("\n");
    }
    printf("%d %c \t", c, c);
  }

  cout << endl << endl;

  cout << "ASCII codes: Part 2" << endl << endl;

  for(e=128; e<=255; e++)
  {
    if(e%8 == 0) {
      printf("\n");
    }
    printf("%d %c \t", e, e);
  }
return 0;
}
using the cpp.sh ( gear icon top right of the code) with all 3 warnings on:

In function 'int main()':
28:15: warning: comparison is always true due to limited range of data type [-Wtype-limits]


The e++ is causing the value to wrap around, try a while loop instead, or break out of the for loop.

This is an example of where one uses the compiler to help them, warnings are your friend :+)
TheIdeasMan, thank you. How could I imply while loop or break into my code?
So can you look up the documentation for break ?

Figuring out things for your self is the best way to learn :+)
TheIdeasMan, thank you! I will try to figure it out myself and if it doesn't work after many hours then will write again:)
Figured it out with while loop. Thanks TheIdeasMan! Now, how it would be possible to get all 255 ASCII symbols on mac terminal? Or should I use text file or another editor? Maybe someone knows which text editor I should use on mac? Thanks. I'll look forward to your replies!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <cctype>            // for isprint
using namespace std;

//======================================================================

void printRange( const char *title, int i1, int i2, int numPerLine, char unprintable )
{
  cout << title << "\n\n";
  for ( int i = i1; i <= i2; i++ )
  {
    char c = i;
    if ( i < 128 && !isprint( c ) ) c = unprintable;
    cout << setw( 3 ) << i << setw( 2 ) << c << "  ";
    if ( ( i - i1 + 1 ) % numPerLine == 0 ) cout << '\n';
  }
  cout << '\n';
}

//======================================================================

int main()
{
  printRange( "Character set   0-127:",   0, 127, 8, ' ' );
  printRange( "Character set 128-255:", 128, 255, 8, ' ' );
}

//====================================================================== 


Anything above 127 will depend on the capacity of your output device to display the character.

I can't see this being very portable.

ASCII only goes from 0-127 ... and there is no guarantee that your system would necessarily use it as the character collating sequence.
Last edited on
Topic archived. No new replies allowed.