开发者

How to decode the value encoded as one*100 + two*10 + three?

How to get values one, two and three back? Is the following approach correct? Looks like no.

int EncodedValue = one*100 + two*10 + three;

// the following decryption is part of another procedure
int one = EncodedValue / 100;
int two = EncodedValue / 10;
int thre开发者_如何学Goe = EncodedValue % 10; 


Assuming two and three are originally in the range 0-9 (otherwise you have ambiguity) you want:

int two = (encodedValue / 10) % 10;

... otherwise it basically gets ten times the one value added to it.


That won't quite work. While I can't speak to the efficiency of the following, it does work:

int encoded = one*100+two*10+three
int one = encoded/100;
int two = (encoded-100*one)/10;
int three = encoded - 100*one - 10*two;

Mind you, this only works if all those variables are a single digit. It could also be implemented recursively to allow for numbers of any length to be decomposed to their digits, but the it may be less intensive to simply convert to a string and then a character array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜