C ++: insert negative character value in string literal?
I have a text renderer that uses negative char values from strings to render native characters in a font that I made instead of plain ascii text.
text.write("Hello! _"); // insert a heart at the underscore somehow...
// the heart value is char -10
Is it possible to get a negative char value written statically inside double quotes? Or is there a way to do this outside of the quotes, but still as a string literal?
Edit:
To clarify a bit, I have to use an array char
in this particular case, unfortunately. The code inside uses std::string
, and all positive values are taken over the standard ascii clays. My goal is to be able to enter a negative number to represent additional characters in a string literal. Thus, the following would be true:
const char * literal = "a literal w/ a negative char value as the 10th element"
literal[9] == -10; // would be true
You can write "Hello! \xf6"
. f6 sixteen= -10 + 256
This works because in 8-bit 2's complement integers, negative values -128 .. -1
have the same bit representations as 256 large unsigned values 128 .. 255
.
I think you misunderstood how positive and negative integers are represented in memory.
Every "unsigned char"> 127 will be interpreted as a signed char <0, that is, when you use non-ASCII characters and interpret them as a signed char, they are automatically negative.
So, I see two common approaches for entering "negative" character values:
- Either you enter characters from another encoding that extend the ASCII encoding and are fixed at one byte per character, for example. "ISO-8859-1". Not a good choice in my opinion as you run into intro issues when the file encoding is not set properly, but that should do the trick.
- Or do you use escape sequences to directly define a byte, eg. you enter some "unsigned char"> = \ x80.
Some links for you:
- http://upload.wikimedia.org/wikipedia/de/5/5c/Zahlenkreis_sint3.jpg
- http://en.wikipedia.org/wiki/Integer_%28computer_science%29
- http://www.joelonsoftware.com/articles/Unicode.html