# How do you attach a 0 before a single digit element in an array?

• 07-15-2009
dauden6
How do you attach a 0 before a single digit element in an array?
Heres an example:
I produce the array 89 9 78 53 2
How do I make it so it reads 89 09 78 53 02

I need this because I am doing an encryption assignment saying:
"A = 0, B = 1, C = 2, …Z = 25

ID is converted to 0803 "

The way it is now I have "ID" = 83 when I need it to be 803.

I could have it as A = 00, B = 01, C = 02,... all the way up to 09 but I don't know how to keep that 0 in front.

There must be a way to do it like... if array[i] < 10 and is the second number... put a 0 before it? I don't really know how to do that.
• 07-15-2009
whiteflags
Use the manipulators fill, to pad, and width, to make room for the highest place value, from the iomanip header.
• 07-15-2009
Spidey
Quote:

I could have it as A = 00, B = 01, C = 02,... all the way up to 09 but I don't know how to keep that 0 in front.
Adding a 0 before a number converts it to octal.
Your best bet is to use manipulators or strings to represent your ID.
• 07-15-2009
dauden6
I should have done this in the original post but this is what the assignment is asking for...

"Recently developed encryption schemes employ one key for encryption and one for decryption. These schemes are called public key encryption schemes because the encryption key is made public by the receiver to all those who transmit messages to him or her; the decryption key, however, is known only to the receiver. The security of these schemes depends on it being nearly impossible to determine the decryption key if one only knows the encryption key. In 1978, Rivest, Shamir, and Adelman proposed one method of implementing a public key encryption scheme. The public key is a pair (e,n) of integers, and one encrypts a message string M by dividing it into blocks M1…Mk and converting each block of characters to an integer Pi in the range 0 through n –1. For the purposes of this program, each block will be two characters and the conversion we will use is A = 0, B = 1, C = 2, …Z = 25. M is then encrypted by raising each block to the power e and reducing mod n.:

Message: M = M1M2…Mk converted to P1P2…Pk

Cryptogram: C = C1C2…Ck, where Ci = Pie mod n

The cryptogram C is decrypted by Pi = Cid mod n where d is the secret decryption key.

To illustrate:

Suppose that (17, 2773) is the public encryption key and the message is “IDESOFMARCH”. To encrypt M using the RSA algorithm, the message is divided into two character blocks (appending a randomly selected character X)

ID is converted to 0803 ---------------- 803^17 mod 2773 = 779

ES is converted to 0418 ---------------- 418^17 mod 2773 = 1983

OF is converted to 1405 ---------------- 1405^17 mod 2773 = 2641

MA is converted to 1200 ---------------- 1200^17 mod 2773 = 1444

RC is converted to 1702 ---------------- 1702^17 mod 2773 = 52

HX is converted to 0723 ---------------- 723^17 mod 2773 = 802

The encoded message is 779 1983 2641 1444 52 802. "
• 07-15-2009
Spidey
Quote:

ID is converted to 0803 ---------------- 803^17 mod 2773 = 779

ES is converted to 0418 ---------------- 418^17 mod 2773 = 1983

OF is converted to 1405 ---------------- 1405^17 mod 2773 = 2641

MA is converted to 1200 ---------------- 1200^17 mod 2773 = 1444

RC is converted to 1702 ---------------- 1702^17 mod 2773 = 52

HX is converted to 0723 ---------------- 723^17 mod 2773 = 802
Notice how the first zero is actually never used in the calculation. That suggests that you can display the conversion a string and then convert that string into an integer to perform your calculations.
• 07-15-2009
Sebastiani
>> I should have done this in the original post but this is what the assignment is asking for...

You're posting all the wrong information. Forget about the encryption bit for just a moment. Explain what format all of the data is in (eg: ints, strings, floats, whatever), and maybe even post a snippet of code demonstrating the problem.
• 07-15-2009
whiteflags
It's probably easiest to build a string for each block like "0803" and then convert it to a number using whatever technique you've learned. From there you can encrypt.
• 07-16-2009
iMalc
I spent a moment trying to figure out how to get 0803 from "ID" And then I realised that I is the 9th letter of the alphabet and D is the fourth letter.
Thus 803 comes from ('I' - 'A') * 100 + ('D' - 'A')