0

I'm trying to work with Tiny Encryption Algorithm, but here is the thing: I'm not getting why the array is a long type and it has hex values, for example,

unsigned long v[] = {0xe15034c8, 0x260fd6d5};

Also, I want to pass a plaintext (ASCII) read from stream, in what data format should I pass it to this type of array?

the encryption function is:

void encipher(unsigned long *const v,unsigned long *const w,
              const unsigned long *const k)
{
    register unsigned long       y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
                a=k[0],b=k[1],c=k[2],d=k[3],n=32;

    while(n-->0)
    {
        sum += delta;
        y += (z << 4)+a ^ z+sum ^ (z >> 5)+b;
        z += (y << 4)+c ^ y+sum ^ (y >> 5)+d;
    }

    w[0]=y; w[1]=z;
}
AliDeV
  • 213
  • 2
  • 10
  • Why shouldn't it have hexadecimal values? What are you asking? And what do you mean by "pass data to the array"? You pass the data to a function that does the encryption, not to an array. – interjay Mar 19 '15 at 18:05
  • @interjay What I mean is that how to pass ASCII characters into the form of long type to the function , so it can encrypt it? – AliDeV Mar 19 '15 at 18:08
  • You didn't show any function, just an array. – interjay Mar 19 '15 at 18:10
  • @interjay I've just added the function. – AliDeV Mar 19 '15 at 18:14
  • It would seem that your instructor skipped (or you didn't understand) how data is actually stored in memory. How the data is stored in memory bears some relationship to the data value. However, 0x40 = b01000000 = 64 = '@' = 0100. and very similar equalities for all the other byte values. you might want to look at: which is all the equalities for one byte for ASCII values (other machines, like the IBM 360, use the EBCDIC character encoding see: for the details – user3629249 Mar 19 '15 at 19:06
  • You should also be familiar with the concept of 'code page' which is defined here: the short explanation of Code Page is: "In computing, a code page is a table of values that describes the character set used for encoding a particular set of glyphs, usually combined with a number of control characters." – user3629249 Mar 19 '15 at 19:10

4 Answers4

1

It has long because 0xe15034c8 is 3780129992 in base-10. On some platforms int is too small to handle such a big number. Moreover, hexadecimal representations often have fewer digits and consumes less space in code than the decimal representations. Here it's said max value for int32 is 2,147,483,647. Your first number (3,780,129,992) is greater while the second one (638,572,245) is smaller.

Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • `0xe15034c8` consumes the same amount of space as `3780129992`, both in the source code (10 chars), and in the generated binary where they are identical. – interjay Mar 19 '15 at 18:08
  • @interjay, oops, I didn't count... Gonna edit this – ForceBru Mar 19 '15 at 18:12
1

unsigned long on most of the platforms will be greater than 4 bytes. So you are storing a 32 bit value in this array which is totally fine.

You can do a sizeof(unsigned long) and see what is the size of unsigned long and calculate what is the maximum value it can hold.

Also, I want to pass a plaintext (ASCII) read from stream, in what data format should I pass it to this type of array?

Presuming that you want to read the input as string and store the value in this array then there are API's like atoi() which you can use

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

Integral constants can be represented in octal, decimal, or hex. The following lines are equivalent and initialize a to 10.

int a = 012;
int a = 10;
int a = 0xA;

In your case,

unsigned long v[] = {0xe15034c8, 0x260fd6d5};

is equivalent to:

unsigned long v[] = {3780129992, 638572245};

Regarding

I want to pass a plaintext (ASCII) read from stream, in what data format should I pass it to this type of array?

You'll need to parse the contents of the string using one of several functions from the standard library, such as sscanf, atoi, strtol, and assign the resulting number to an element of v.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

This is a common confusion. Hexadecimal is not a data type, it's a representation. For example, a "table" can be called differently on other spoken languages (i.e. "mesa" in spanish) but it is still a table. In this case, values (e.g. 10) can be represent in different ways, the most common for us humans is base 10. But computers use binary (and other related representations like hexadecimal) and thus what for us is 10, for the computer it is 1010 (A in hexadecimal). At the end, the long is just a 32-bit value, how you want to represent it is up to you, but the value is the same.

For characters, for the computer they are still numbers, so you can just pass their values as chunks of longs (i.e. using atoi())

m0skit0
  • 25,268
  • 11
  • 79
  • 127