?
Solved

urgent : ascii values

Posted on 2010-01-12
15
Medium Priority
?
273 Views
Last Modified: 2012-05-08
hi,

i am writing a program where i am using the ascii value as the index of the array. my problem is that i am getting negative value of the index...i thought all the available characters should be from positive only for ascii values...

thanks a lot.
0
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 26300576
Did you mean printable ascii chars?

Well, A-Z, a-z, 0-9, and usual punctuation and whitespaces and some control chars are positive. But the Extended ASCII Codes are > 127. (See http://www.asciitable.com )

If your char is signed, that could appear as negative. Are you using:
unsigned char
or just
char
?
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 26300578
any code to share?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26300580
You can post your code (use the Code button) and maybe we can assist better.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zizi21
ID: 26300853
hi, its for my assignment..that's why, i cant post it here but i could give a snippet...

for(int i=0; i <n; i++)
      {
            if(string[i] < 0){
                  printf("Negative... %d \n",string[i]);
                  exit(1);
            }      
      }
0
 

Author Comment

by:zizi21
ID: 26300859
i am using unsigned char...why is it appearing negative then...

this is what i got:

Negative... -110

0
 

Author Comment

by:zizi21
ID: 26300863
Negative... -110 ?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26300935
If you have:
unsigned char string[100];
then I agree that (string[i] < 0) is always false

Not that it should matter, but n is within boundary of string array, right?
Also, since string is a keyword, let's change it to myString just to be sure there is nothing weird going on.
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 26300945
how do you define string?
0
 

Author Comment

by:zizi21
ID: 26301154
i have defined as char and not unsigned char..
0
 

Author Comment

by:zizi21
ID: 26301160
you mean, i cant typecast it as unsigned int as welll? it needs to be unsigned char...
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26301164
then define it as unsigned char since you want it positive.
what values are you sticking in string[i] - how large?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26301205
You are using c. I recall that in C that char is not strictly portable from one platform to another w.r.t. signed. It usually, by default, is signed; but there's a compiler option on some compilers to force it to be unsigned. So better to be explicit - you want non-negative numbers, so make it unsigned.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 26301321
re: "you mean, i cant typecast it as unsigned int as well?"
You can typecast a char as unsigned int. But do not be surprised if a cout gives you odd results - if you take a char whose value is negative, and then cast it as (unsigned int), you may find yourself with a very large positive number since the sign bit may be extended to fill up the full 32 bits of the int.

recommend:
unsigned char string[100];
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 26301876
>> i am writing a program where i am using the ascii value as the index of the array.

What ascii values are you talking about here ? Where do they come from ?
0
 
LVL 3

Expert Comment

by:Greg_Arnold
ID: 26304745
sometimes the sign bit gets extended even on unsigned chars, so try this to make sure the sign bit isn't extended.

unsigned int i;
unsigned char c = 130;
i = ((unsigned int)c) & 0xff;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question