task9 Write a C program printing to sdout all its arguments inluding argv[o]) he
ID: 3745180 • Letter: T
Question
task9 Write a C program printing to sdout all its arguments inluding argv[o]) hex-encoded. You must save your code in a file called task9.c. You must include a script called compile.sh that, when invoked, compiles the code in task9.c and generates an executable file called task9. Do not include this executable file in your submission tar file. You must come up with your own hex-encoding function. Do not just copy it from StackOverflow! Examples (assuming you are opening your program from bash) If the program is invoked in the following way: ./task9 The output should be: 2e2f7461736b39 I suggest reading the top answer here: https://stackoverflow.com/questions/ 8009664/how-to-split-a-delimited-string-into-an-array-in-awk If the program is invoked in the following way ./task9 firstargument The output should be: 2e2f7461736b39 6669727374617267756d656e74 If the program is invoked in the following way: ./task9 first argument with spaces The output should be: 2e2f7461736b39 666972737420617267756d656e74207769746820737061636573 If the program is invoked in the following way: ./task9 hello $'aneline nishere' The output should be 2e2f7461736b39 68656c6c6f 616e65776c696e650a697368657265Explanation / Answer
//below is the C program save it as task9.c
#include <stdio.h>
void StringToHex(unsigned char* string, unsigned char* hexstring)
{
unsigned char ch,i,j,len;
len = strlen(string);
for(i=0,j=0;i<len;i++,j+=2)
{
ch = string[i];
if( ch >= 0 && ch <= 0x0F)
{
hexstring[j] = 0x30;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x10 && ch <= 0x1F)
{
hexstring[j] = 0x31;
ch -= 0x10;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x20 && ch <= 0x2F)
{
hexstring[j] = 0x32;
ch -= 0x20;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x30 && ch <= 0x3F)
{
hexstring[j] = 0x33;
ch -= 0x30;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x40 && ch <= 0x4F)
{
hexstring[j] = 0x34;
ch -= 0x40;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x50 && ch <= 0x5F)
{
hexstring[j] = 0x35;
ch -= 0x50;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x60 && ch <= 0x6F)
{
hexstring[j] = 0x36;
ch -= 0x60;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
else if( ch >= 0x70 && ch <= 0x7F)
{
hexstring[j] = 0x37;
ch -= 0x70;
if(ch >= 0 && ch <= 9)
hexstring[j+1] = 0x30 + ch;
else
hexstring[j+1] = 0x37 + ch;
}
}
hexstring[j] = 0x00;
}
int main(int argc, char* argv[])
{
int length = strlen(argv[1]);
char hexval[2*length];
StringToHex(argv[1], hexval);
printf("your hex val is %s",hexval);
}
//end of C program
//*******save the below given commands in file and name that file as compile.sh*****
#!/bin/bash
gcc -o task9 task9.c
// Run the below command
sh compile.sh
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.