ASCII 30, ASCII 31, '

Oct 16, 2008 at 10:35pm
Hello guys, I have a few quick (I should hope..) questions.

The assignment is that I'm to read in a file of an encoded message and decode it and print it.

I'm hung up on this:

I'm checking for

a) an ASCII 30 character, which I am supposed to change to a space (" ")
b) an ASCII 31 character, which I am supposed to change to a new line (\n)
c) a 4, which should become an apostrophe

the problems are here:

(this is a function)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char checkSpecial(char ch)
{
		
	if (ch=='1E')
		{
		ch = ' ';
		return ch;
		}
	else if (ch=='1F')
		{
		ch = '\n';
		return ch;
		}
	else
		return ch;
		
}


This won't work. 1E and 1F are clearly not the way to go. What is?

And for c).

1
2
case '4':
ch = '''; 


How'm I supposed to put that in there? It's clearly messing it up by having three apostrophes in a row.

Thanks a million for anyone who has something to add.

-Andrew
Last edited on Oct 16, 2008 at 10:35pm
Oct 17, 2008 at 12:17am
To check a character, you can either do:

1
2
3
ch == ## (i.e. ch == 30)
//or
ch == 0x## (i.e. ch == 0x1E) 


To input characters like " ' inside of a string literal, put an escape slash (\) before it:

 
"The \"second\" word of this sentence has \"s around it."


Oct 17, 2008 at 3:08am
Thanks draco, but I still can't get the ASCII 30 and ASCII 31 pieces right.

They show up in the text file I was given as a triangle and an upside down triangle. Is 0x1E and 1F the proper thing to have it look for? I still have no spaces or new lines in my output.
Oct 17, 2008 at 3:23am
Hmm, did you try just looking for ch == 30/31?
Oct 17, 2008 at 3:29am
Just gave it a shot, but no-go.

This is the information I was given:
#

Two white space characters were encoded as non-standard characters:

a blank (' ') was encoded as an ASCII 30
a newline character ('\n') was encoded as an ASCII 31


And the file that I am decoding is this:

http://www.cs.niu.edu/~mcmahon/cs240/Assign/code.txt
Last edited on Oct 17, 2008 at 3:30am
Oct 17, 2008 at 3:33am
Hmmm, try \30 or \0x1E....it probably won't work but you might as well give it a try.

EDIT: You could also try:

if (ch == (char) 30)
Last edited on Oct 17, 2008 at 3:40am
Oct 17, 2008 at 12:43pm
That didn't work either :(

This is insane! Did he tell me the right ASCII character number? Because if they are indeed 30 and 31, why wouldn't any of your solutions have worked...
Oct 17, 2008 at 2:34pm
You are making a bit of a meal of this.
That code.txt file does contain values of hex 1E and 1F.

As others have already said you character replace function should
look more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char checkSpecial(char ch)
{
    if (ch== 0x1E)
    {
        ch = ' ';
        return ch;	
    }
    else if (ch== 0x1F)
    {
        ch = '\n';
        return ch;
    }
    else
    return ch;
		
}


maybe you are also having issues reading the file correctly??
Topic archived. No new replies allowed.