Flavius
Flavius

Reputation: 13816

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

Upvotes: 4

Views: 11971

Answers (8)

Jens Gustedt
Jens Gustedt

Reputation: 78943

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.

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

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

Upvotes: 0

KevinDTimm
KevinDTimm

Reputation: 14376

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

Upvotes: 4

wnoise
wnoise

Reputation: 9942

For actual code, you should be library functions, such as toupper() or towupper(), or something able to handle the complexity of Unicode.

Upvotes: 3

Steed
Steed

Reputation: 287

try and with 0xDF (hex) or 011011111 binary

Upvotes: -1

Šimon Tóth
Šimon Tóth

Reputation: 36451

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

Upvotes: 2

schnaader
schnaader

Reputation: 49729

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.

Upvotes: 15

semaj
semaj

Reputation: 1575

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.

Upvotes: 0

Related Questions