I've been checking forums alot lately, and it helped me in more than once while facing some C++ stuff. and my first time posting.
Anyway, am having an assignment where i need to decode and encode a ZipCode, the input of the BarCode should be as a string. BarCode contains 27 digits with 1 at the beginning and at the end of the string which will be left alone and we'll have only 25 digits formed by 1's and 0's. lets say this is the Barcode 1 10100 11000 01010 01100 10100 1.
to decode we'll take 10100 and we multiply by 74210 which will give me ( 1x7 + 0x4 + 1x2 + 0x1 + 0x0 ) = 9 which is the first digit of the ZipCode.
am stuck with how to multiply each 5 digits with (74210), coz as i far as i know strings use characters and not integers, is there any way to do this ? and as you can tell, am not a pro, just a beginner :)

thanx in advance

9 Years
Discussion Span
Last Post by Ancient Dragon

You're right, strings contain (ASCII) character codes, not integers. So in this case you need to consider each character in the string and convert it to an integer 1 or 0 before performing your arithmetic.
There are a variety of ways to do the conversion, check out atoi() for starters.


integer digits are also characters. For example '0', '1', '2', ... '9' are all ascii characters just as 'a', 'b', ... 'z' are ascii characters.

Its quite easy to convert an ascii numeric character to an integer -- just subtract '0'

int x = '0' - '0'; // result is 0
x = '1' - '0'; // result is 1

Why does that work? Look at any standard ascii chart and you will see that every character in the ascii chart has a numeric equivalent. When you code x = '1' - '0' the compiler looks up those charcters in the ascii chart and converts them to their numeric equivalents x = 49 - 48;

Votes + Comments
nice, didn't know it was possible ... very useful post !
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.