I need to get the acutal glyph value, not the ascii value, of a char in c++. Google isnt helping. Will you halp? What if i promise to read my SICP every day?
To clarify, if, for example, i want the value of the glyph for '2', i want the integer value 2, not its ascii decimal value of 50.
Name:
Anonymous2008-03-22 0:39
'2' - '0'
Name:
Anonymous2008-03-22 2:17
Ok, now I want to do the opposite; i get an integer, i then want to convert to its corresponding ascii glyph, then append to a string.
//add 48 to get the ascii value of the integer (0 to 9)
character = num_o + 48;
result = static_cast<char>(character) + result;
Except the output of this is:
;6<
WTF??!?
Name:
Anonymous2008-03-22 2:18
That's quite possibly the easiest thing you could figure out by yourself.
Name:
Anonymous2008-03-22 2:21
static_cast<char>
....
Name:
Anonymous2008-03-22 2:29
Disregard that, I suck dicks. But now my output is "162". Sigh. Thats what I get for trying to be productive. (it should be 1061).
Name:
Anonymous2008-03-22 2:31
int main()
{
string m, n, result = "";
int index, num_m, num_n, num_o, difference, character;
char cvalue;
bool carry;
//make the two strings equal in length by adding the correct amount of 0's to the shorter one
if(m.length()> n.length())
{
difference = m.length() - n.length();
do
n = "0" + n;
while(m.length() > n.length());
}
else if(n.length() > m.length())
{
difference = n.length() - m.length();
do
m = "0" + m;
while(n.length() > m.length());
}
else//do nothing
{
}
cout << n << endl << m << endl;
for(index = m.length()-1; index >=0; index--)
{
//convert the indexed value to a char, then static cast to an int, then subtract ascii'0' to give value
cvalue = m[index];
num_m = static_cast<int>(cvalue);
num_m = num_m - 48;
//add the two values at the same index in m and n
if (carry)
{
num_m = num_m + 1;
}
//add values
num_o = num_m + num_n;
//set the carry flag true, then set num_o to itself - 10 because the carry flag represents the 10 you subtract
if(num_o >=10)
{
carry = true;
num_o = num_o - 10;
}
//add 48 to get the ascii value
character = num_o + 48;
result = (char)character + result;
character = 0;
}
cout << result << endl;
system("pause");
return 0;
}
Name:
Anonymous2008-03-22 2:32
For those interested, I'm trying to add 2 positive integer values of arbitrary length.
Sorry to spam up /prog/ with my irrelevent shit.
Name:
Anonymous2008-03-22 2:35
result = static_cast<char>(character) + result;
What are you trying to do here? Append to a string or something? It doesn't work like that.
Name:
Anonymous2008-03-22 2:38
I was doing a fail append a character to a string. Changed to
result = (char)character + result;
Which is probably equally wrong.
Name:
Anonymous2008-03-22 3:15
you can't append to multibyte strings with +
Name:
Anonymous2008-03-22 3:19
oh I didn't see your other post, so your not using multibyte strings
Name:
Julie Sussman2008-03-22 3:27
>>1
Have you read SICP? It's all explained in there...
Name:
Anonymous2008-03-22 4:58
There's this little known function atoi()...
Name:
Anonymous2008-03-22 5:50
>>8 int alen = strlen(a);
int blen = strlen(b);
int len = max(alen,blen);
for(i=0;i<max;i++)
c[i]=(a[i]+b[i]+carry)%10;
carry = (a[i]+b[i])/10;
}
+ for(i=0;i<strlen(a)&&isdigit(a[i]);i++)
a[i]-=48;