Well as the title ...

example:

string ss="this is an encrytpion program";
unsigned char temp[32]; //={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

temp=ss //that's what i need !

Have converted the string to it's hex representation ..

char string1[64]="yeah working yeah working yeah working yeah working yeah wcccn";
	char number;
	int k=0;
	for(;k<63;k++)
	{
		sscanf(&string1[k], "%c", &number);
		fprintf (f1, "%x", number); //to file
	}

but not able to convert that hex array[64] to unsigned char array[32] ...

> string ss="this is an encrytpion program"; Could it be that you meant: char ss[] = "this is an encrytpion program"; here?
Otherwise please clarify.

> string ss="this is an encrytpion program"; Could it be that you meant: char ss[] = "this is an encrytpion program"; here?
Otherwise please clarify.

Besides your correction of the non-existent string type, I can't make any sense of the OP's question?

I think (he or she) is "out with the fairies" - so to speak.
:-/

>I can't make any sense of the OP's question?
Well, I'm not sure what he's trying to do (as he didn't provide an illustrating example).
I think he wants to convert a 'readable' string to a string containing each character's hexadecimal equivalent, but the string he's trying to use in his 'example' is not big enough for that.

An example of what I think he wants to do:
string to convert: "abcdef"
converted string: "616263646566"

An example of what I think he wants to do:
string to convert: "abcdef"
converted string: "616263646566"

You might be on to something there. If your assertion is correct, then Tom Gunn has already provided an adequate response to the OPs other similar post here (with some minor modifications):

http://www.daniweb.com/forums/thread207833.html

Cheers,
JD

igot what exactly you want:
i think you want to convert your test string into hex code and store that in a file.(encryption)
Again decrypt that and read as your previous string form that file. right?

your problem:
------------
while converting a char to hex 1 byte takes 2 bytes in the file.

you can try like this to decrypt.
step:
1)read 2 byte at a time
2)scan two bytes a hex
3)convert hex to char
Here is an example code(your code is modified): you can look into that and enhance it:

int main()
{

FILE *fp;
char str[3];
fp=fopen("test.txt","r"); //text.txt is the file generated by your encryption prog
int a;
//char *ss="this is an encrytpion program";
unsigned char temp[32];

while(!feof(fp))
{
fread(str,2,1,fp);
str[2]='\0';
sscanf(str,"%x",&a);
printf("%c\n",a);

}
fclose(fp);
return 0;
}

you do the encryption by your self.

Well if you think you understood the OP's question, then I query the integrity of your code snippet. You are reading two characters from the file and trying to convert to hex. Perhaps something like this is what you meant:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    FILE *fp;
    int c;

    if ((fp = fopen("test.txt", "r")) == NULL) {
        printf("Error opening file!");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(fp)) != EOF) {
        printf("%x", c);
    }

    fclose(fp);
    return 0;
}

Cheers,
JD

Hey all,
Posting code based on assumptions is totally useless.
Why we take pain when post starter is not bothered at all, after so many assumptions posted.

Well if you think you understood the OP's question, then I query the integrity of your code snippet. You are reading two characters from the file and trying to convert to hex. Perhaps something like this is what you meant:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    FILE *fp;
    int c;

    if ((fp = fopen("test.txt", "r")) == NULL) {
        printf("Error opening file!");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(fp)) != EOF) {
        printf("%x", c);
    }

    fclose(fp);
    return 0;
}

Cheers,
JD

No this is not the proper code.look at my code carefully and read my last post.

Try to get he encryption decryption concept.

else view my next post .....

run my encryption and decryption code.

@tux4lyf:

i am not putting importance on file functions so missed
fp==NULL check and used !feof()
i only wanted to so the encryption/decryption concept.

Encryption code:
----------------
1)input:"this is an encrytpion program";
2)output:in.txt

contents in.txt:
---------------
7468697320697320616e20656e6372797470696f6e2070726f6772616d

#include<stdio.h>
int main()
{
FILE *fp;
char *ss="this is an encrytpion program";
fp=fopen("in.txt","w");
int i=0;
while(*(ss+i)!='\0')
{
fprintf(fp,"%x",*(ss+i));
i++;
}
fclose(fp);
return 0;
}

Decription CODE:
----------------
1)input:in.txt file
2)output:string ""this is an encrytpion program"

#include<stdio.h>
int main()
{

FILE *fp;
char str[3];
fp=fopen("in.txt","r"); 
int a;

unsigned char temp[32];

while(!feof(fp))
{
fread(str,2,1,fp);
str[2]='\0';
sscanf(str,"%x",&a);
printf("%c\n",a);

}
fclose(fp);
return 0;
}

@tux4lyf:

i am not putting importance on file functions so missed
fp==NULL check and used !feof()
i only wanted to so the encryption/decryption concept.

Only that proves to me that you didn't get what they describe in that link :)
It's not because you quickly want to test out some code that you may use bad coding styles.
Bad coding styles are considered: bad, so you don't have to use them, under any circumstances.
It would also not be a bad idea to format your code when you post/write it.

It's not because you quickly want to test out some code that you may use bad coding styles.
Bad coding styles are considered: bad, so you don't have to use them, under any circumstances.

That rationalization might work when you preach to the choir, but everyone else needs a better reason. :) The better reason is that you are not just a tutor but a role model too. If you use bad coding practices then those practices will be emulated. If you use good coding practices then *those* practices will be emulated. Set a good example because you do not need to create more questions by training weak programmers. There are already plenty. ;)

Comments
The better example is in place here :)

Well Sorry for late reply , some net problems at home, and still are ..
Here's what i am trying to do ..

AES encryption and decrytpion:

1. 128 key
2. ECB mode.

And thats what i have done so far:

1. Encrytpion in VC 6
2. Decrytpion in java
3. keys and cipher text is being send through TCP sockets

But above mentioned things are irrelevant to what i am stuck at:

BACKGROUND:

i am taking user's input of Plane text and Key in

unsigned char Key[32]; //={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};

unsigned char PT[32]; //={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};

for(i=0;i<16;i++)
{
	scanf("%x",&PT[i]); //Plane text in HEXADECIMAL
}

for(i=0;i<Nb*4;i++)
{
	scanf("%x",&Key[i]); //Key in HEXADECIMAL
}

and user input some thing like :

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f

and got Cypher Text like

0a940bb5416ef045f1c39458c653ea5a

NOW:

I want the user to Enter Key and Plane text like a simple string for example;

char key[]="this is my key";
char PT[]="i want to encrypt this";

""And i do the conversion from string -> hex -> unsigned char array myself in program""

----

Now i have done string ->hex part

//String to hex
f1 = fopen ("CT.txt", "wt");  
char string1[64]="A simple string A simple string A simple string A simple string";

char number;
int k=0;
printf("\n");
for(;k<63;k++)
{
	sscanf(&string1[k], "%c", &number);
	fprintf (f1, "%x", number);
}

fclose (f1);

f1 = fopen ("CT.txt", "r"); 
while(!feof(f1))
	if(fgets(string1, 200, f1)){}
fclose(f1);
printf("hex it is %s", string1);

And the output is

412073696d706c6520737472696e6720412073696d706c6520737472696e6720412073696d706c6520737472696e6720412073696d706c6520737472696e67

.....

Now unable to convert that hex->unsigned char array simply because

string[64] wont be assigned to PT[32]

....

I tried Dream2Code's code n the output is

A simple string A simple string ... //string1[64]

-- I NEED TO CONVERT THAT HEX[64] REPRESENTATION OF STRING TO UNSIGNED CHAR ARRAY[32] --

Tried but failed .. so tell me what to do now ... Or is there something that i am doing wrong ...

Many thanks for your replies though ...

I have no idea about any available method to convert Hexa String to unsigned char. But can also be done manually.

maintain a array for all possible hexa numbers :

char hexa[] = {'0', '1', '2', '3',..........,'a', 'b', 'c','d', 'e', 'f'};

And the function which will return int for hexa char:

int getUChar(char hexChar) {
   char hexa[] = {'0', '1', '2', '3',..........,'a', 'b', 'c','d', 'e', 'f'};
   int i;
   for(i=0; i < 16; i++) {
        if(hexa[i] == hexChar) {
             return i;
        }
   }
   return -1;
}

int getDecimal(char hsb, char lsb) {
    int h = getUChar(hsb);
    int l = getUChar(lsb);
    int decimal = (h<<4)+l ;
    return decimal;
}

Now let say your hexa string is:

char *hexaString = "412073696d706c6520737472696e6720412073696d706c6520737472696e6720412073696d706c6520737472696e6720412073696d706c6520737472696e67";

Then pick 2 chars at a time from above hexa string say:

#include<stdio.h>
#include<ctype.h>

int main ( ){
    char *bigHexa = "412073696d706c6520737472696e6720412073696d706c6520737";
    char decimalOfHexa = [100];
    int i = 0, j = 0;
    while(true) {
        decimalOfHexa[j] = getDecimal(bigHexa[i], bigHexa[i+1])
         if(bigHexa[i+2] == '\0') break;
        i = i + 2;
    }
}

Basically the idea is as follows:

lets say u have this hexaString: "4120". Then it is clear that we have 2 unsigned chars ie '41' and '20' (Hex). So what I am trying to do is picking 2
characters say '4,1' from hexaString (which is actually 1 unsigned char - 1 byte), and converting '4' and '1' to there integers representation.
And adding them both to get 1 unsigned character (((4<<4) + 1 = 65 ), now 65 is first unsigned char and same is done with '20'.

Hey chap ... Let me Thankuu for ur reply first ..

Now before i copy paste that code, test it and understand the logic if it works .. let me ask you before hand that .. "So you saying decimalofHexa[32] IS the unsigned char representation of string1[64] from my code??

And what if i want the same conversion but opposite in JAVA ??

i.e hexarray[64]=unsigned char array[32]; // JAVA

Is there some function available hidden out there that i don't know of ??

P.S. Will reply soon if the net in home will allow me ..

Thx once again ..

I just wrote this code to give you an idea for how to do conversion manually (In fact this logic can be used in any language).
you may have to change the code as per you requirements: ...

Done some changes for unsigned char and j++

#include<stdio.h>
#include<ctype.h>
int main ( ){
    char *bigHexa = "412073696d706c6520737472696e6720412073696d706c6520737";
    unsigned char decimalOfHexa = [100]; // change
    int i = 0, j = 0;
    while(true) {
        decimalOfHexa[j] = getDecimal(bigHexa[i], bigHexa[i+1])
         if(bigHexa[i+2] == '\0') break;
        i = i + 2;
        j++; // change
    }
}

In java I have least experience so no idea about built in functions.

This question has already been answered. Start a new discussion instead.