简体   繁体   中英

Lower case to upper case

How to convert lower case ASCII char into upper case using a bitmask (no -32 allowed)?

I'm not asking for solving my homework, only some hints.

Thanks

As you state "(no -32 allowed)", I guess you know that the difference between lower case characters and upper case characters is 32. Now convert 32 to its binary representation, there's only one bit set. After that, work out a way to use a bit mask to switch the bit.

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

XOR to get lower from upper or upper from lower

对于实际代码,您应该是库函数,例如toupper()或towupper(),或者能够处理Unicode复杂性的东西。

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

This example assumes that the string is in ASCII, and using the English alphabet.

This is C99 C code, you should use the proper compiler flag to set this when compiling. I specifically tried not to use any libs in this example, standard or not because I'm guessing you're still in the process of 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 takes advantage of the fact that the ONLY difference between an upper and a lower case character in the ASCII table, is a single bit. Specifically the 6th bit (from the right.) All we have to do is create a "mask" that contains all 1's except for the 6th bit (from the right) and then use the binary AND instruction (&) to apply this mask to our character. And then of course put this into our string.

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 a single ASCII character) upper case in c. Unless, of course you want something that will return a new string, ie you want to create an upper-case version of the "old" string but still be able to keep the original version somewhere. This shouldn't be too hard to do, if you understand my first example. You just have to allocate a new array to put the upper-case string in, and return a pointer to this array (unsigned char*).

Compare the hexadecimal values of lower case ASCII characters to upper case ASCII characters and the solution should become clear. It may also be helpful to compare the binary values if the solution is not evident right away.

从一个小拉丁字母的ASCII码中减去32的操作将第5位从1翻转到0。

As you specify this, your homework is not well defined. The C standard knows nothing about a particular encoding of the source or execution character set, in particular it doesn't assume anything that comes close to ASCII or so.

So wnoise was right, the only standard way to deal with these things are the predefined functions and macros that are provided for such an effect.

尝试使用0xDF(十六进制)或011011111二进制

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM