As everyone knows, for a console program commandline arguments are passed to the program as an array of strings.
C:\>program.exe 123456
To use 123456 as a numerical data type, library functions are commonly used to convert the string.
int i;
i = atoi(argv[1]);
or
unsigned long i;
i = strtoul(argv[1]);
I couldn't find a library function in native C for converting a string to a byte array. Searching the internet for a method mainly turns up the silly answer that C stores strings as a byte array so there is no need to convert them.
Here is a simple way to convert a fixed length string to a byte array. I chose a 32 character string to use as a 16 byte key.
output:
C:\>program.exe 123456
To use 123456 as a numerical data type, library functions are commonly used to convert the string.
int i;
i = atoi(argv[1]);
or
unsigned long i;
i = strtoul(argv[1]);
I couldn't find a library function in native C for converting a string to a byte array. Searching the internet for a method mainly turns up the silly answer that C stores strings as a byte array so there is no need to convert them.
Here is a simple way to convert a fixed length string to a byte array. I chose a 32 character string to use as a 16 byte key.
C:
// convert_to_byte.c : Defines the entry point for the console application.
//
//converts a string which is 32 characters long to a byte array
#pragma warning(disable : 4996)
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
unsigned char input_string[33]; //a string variable for holding the input string
unsigned char little_strings[16][3]; //split input string into 16 little strings
int i; // i and j are counters for the loops
int j;
unsigned __int8 byte_array[32]; //for the array of bytes
printf("\nconvert_to_byte.exe version 0.1.1\n");
//check number of command line arguments
if(argc !=2) {
printf("\n\nusage: %s input_string", argv[0]);
printf("\ninput_string is a 32 digit string of numbers...\n\n");
return 0;
}
//check if length of input number is 32 digits
if((strlen(argv[1])) != 32) {
printf("\n\nthe input number must be 32 digits...\n\n");
return 0;
}
//print argv[1] as string and as characters
printf("\nargv[1] as\n string %s\n characters ", argv[1]);
for(i=0;i<32;i++) printf("%c ", argv[1][i]);
//copy string from argv[1] to a string 32 characters plus null to terminate the string
//not necessary, but provides a level of abstraction from the command line
memcpy(input_string, argv[1], 33);
printf("\n\ninput_string as\n string %s\n characters ", input_string);
for(i=0;i<32;i++) printf("%c ", input_string[i]);
//copy 2 characters at a time from the input string into 16 little strings
printf("\n\ncopying 32 characters, 2 at a time, from the string into 16 little strings...");
for(j=0,i=0; j<16; j++,i+=2 ) {
little_strings[j][0] = input_string[i]; //2 characters to make 2 digits of byte
little_strings[j][1] = input_string[i+1];
little_strings[j][2] = '\0'; //null character terminates a string
}
//convert array of strings to array of byte values
//by using the library function strtoul to convert a string to unsigned long
//and using a type cast to convert unsigned long to byte, an unsigned 8 bit int
printf("\n\nconverting each little string to a byte...");
for(j=0;j<16;j++) byte_array[j] = (unsigned __int8)(strtoul(&little_strings[j][0],NULL,16));
//print little strings as strings
printf("\n\nindex ");
for(j=0;j<16;j++) printf(" %2d",j);
printf("\nlittle_strings");
for(j=0;j<16;j++) printf(" %s", &little_strings[j][0]);
//print byte array
printf("\nbyte_array ");
for(i=0;i<16;i++) printf(" %.2X", byte_array[i]);
printf("\n\nthe little strings were printed using %%s");
printf("\nthe bytes were printed using %%X");
//for the address of the string
//which was written: &little_strings[j][0]
//can also use the short form: little_strings[j]
printf("\n\n\nfinished...\n\n");
return 0;
}
C:
C:\>convert_to_byte 0123456789ABCDEF0123456789ABCDEF
convert_to_byte.exe version 0.1.1
argv[1] as
string 0123456789ABCDEF0123456789ABCDEF
characters 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
input_string as
string 0123456789ABCDEF0123456789ABCDEF
characters 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
copying 32 characters, 2 at a time, from the string into 16 little strings...
converting each little string to a byte...
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
little_strings 01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF
byte_array 01 23 45 67 89 AB CD EF 01 23 45 67 89 AB CD EF
the little strings were printed using %s
the bytes were printed using %X
finished...
Last edited by a moderator: