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

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    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.
    Last edited by dauden6; 07-15-2009 at 10:22 PM. Reason: Added details

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use the manipulators fill, to pad, and width, to make room for the highest place value, from the iomanip header.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    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. "
    Last edited by dauden6; 07-15-2009 at 11:16 PM.

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    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.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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')
    As it turns out, this exactly answers what you're asking about, as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. Deleting element from an array problem
    By xamlit in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 04:53 PM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM