Lower case to uppercase

How to convert lowercase ASCII char to upper case using bitmask (-32 not allowed)?

I do not ask permission of my homework, only some tips.

thanks

+4
source share
9 answers

As you declare "(-32 is not permitted"), I think you know that the difference between lowercase characters and lowercase letters is 32. Now we convert 32 to its binary representation, there will be only one bit. After that, develop a way to use the bit mask to switch the bits.

+14
source

Think about the difference between lower and upper case (0x20), and then apply the appropriate mask to your value

XOR to go below the top or top level below

+4
source

Just translate +-32 into a bit operation. 32 can be written as 2^x .

+2
source

For the actual code, you have to be library functions like toupper () or towupper () or something that can handle Unicode complexity.

+2
source

This example assumes the string is in ASCII and the English alphabet is used.

This is C99 C code, you must use the appropriate compiler flag to set this when compiling. I specifically tried not to use any libraries in this example, standard or not, because I assume that you are still learning the basics of C programming.

 #define UPPER_CASE_SWITCH 0x5f void makeUpper(unsigned char *string, int length) { for(char c; length != 0 && (c=*string) != 0; --length) *string++ = (((c >= 'a' && c <= 'z')) ? (c & UPPER_CASE_SWITCH) : c); } 

It exploits the fact that ONLY the difference between an upper and lower case character in an ASCII table is one bit. In particular, the 6th bit (right). All we need to do is create a β€œmask” containing all 1 except for the 6th bit (on the right), and then use the binary AND (&) instruction to apply this mask to our character. And then of course put this on our line.

Here is a python example.

 >>> bin(ord("a")) ## Gets the binary digit for the letter "a" '0b1100001' >>> bin(ord("A")) ## Gets the binary digit for the letter "A" '0b1000001' >>> hex(0b1011111) ## Gets the hexadecimal mask we are using in the C source '0x5f' 

In my opinion, this is the best way to make an ASCII string (or one ASCII character) in c. Unless, of course, you need something that will return a new line, then you want to create a version of the "old" line in upper case, but you can still save the original version somewhere. This should not be too hard to do if you understand my first example. You just need to select a new array to insert a string string, and return a pointer to that array (unsigned char *).

+1
source

Compare the hexadecimal values ​​of the lowercase ASCII characters with the upper case ASCII characters, and the solution should be clear. It may also be useful to compare binary values ​​if the solution does not immediately appear.

0
source

The operation of subtracting 32 from the ASCII code of the small Latin letter flips the 5th bit from 1 to 0.

0
source

As you indicated this, your homework is not defined. The C standard does not know anything about the specific encoding of the source or executable character set; in particular, it does not imply anything that comes close to ASCII or so.

So wnoise was right, standard the standard way to handle these things is with the predefined functions and macros that are provided for this effect.

0
source

try with 0xDF (hexadecimal) or 011011111 binary

-1
source

All Articles