I am newbie to this topic.
I am trying to convert integer array to string. Then string to integer array to check if I am getting same input.
gint16 frame[5] = {10, 2, 3, 7, 5};
char *str = malloc(sizeof(char) * (sizeof(frame)+1));
char *strp = str;
size_t j;
for (j= 0; j < sizeof(frame); j++) {
snprintf(strp, 4, "%02x", frame[j]); //hexadecimal
strp++;
}
// from hexa string to 16 bit integer array
gint16 n_oframe[5];
size_t i_m;
for (i_m = 0; i_m < 5; i_m++) {
char *d = (char*)malloc(sizeof(gint16));
strncpy(d,str,2);
n_oframe[i_m] = atol(d);
str = str + 2;
free(d);
}
When I try to print out n_oframe values, I am getting in correct results. Please help me
Use these functions:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef int16_t gint16; // define gint16 if not compiling with glib.h
char *gint16_to_string(gint16 *p, int n) {
char *str = malloc(n * 4 + 1);
if (str) {
for (int i = 0; i < n; i++) {
snprintf(str + i * 4, 5, "%04X", p[i] & 0xFFFF);
}
}
return str;
}
void string_to_gint16(gint16 *p, int n, const char *str) {
if (str) {
for (int i = 0; i < n; i++) {
unsigned int x = 0;
sscanf(str + i * 4, "%4x", &x);
p[i] = (gint16)x;
}
}
}
int main(void) {
gint16 frame[5] = { 10, 2, 3, 7, 5 };
// encoding in hexadecimal
char *str = gint16_to_string(frame, 5);
printf("encoded string: %s\n", str);
// from hexa string to 16 bit integer array
gint16 n_oframe[5];
string_to_gint16(n_oframe, 5, str);
printf("n_oframe: ");
for (int i = 0; i < 5; i++) {
printf("%d, ", n_oframe[i]);
}
printf("\n");
free(str);
return 0;
}
The commenters found most of it, so to put it all together
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
// ALL CHECKS OMMITTED!
int main()
{
int16_t frame[5] = { 10, 2, 3, 7, 5 };
// hexadecimal string = 2 characters plus NUL
// sizeof(char) == 1, if compiler is standard compliant
char *str = malloc(3 * (sizeof(frame)/sizeof(frame[0]) +1));
char *strp = str;
size_t j;
for (j = 0; j < sizeof(frame)/sizeof(frame[0]); j++) {
// again: hexadecimal string = 2 characters plus NUL
snprintf(strp, 3, "%02x", frame[j]); //hexadecimal
strp += 2;
}
// we need a pointer to the start of the string to free it later
strp = str;
// let's see if we gott all of them
printf("str = %s\n",str);
// from hexa string to 16 bit integer array
int16_t n_oframe[5];
size_t i_m;
// and again: hexadecimal string = 2 characters plus NUL
// a simple char d[3]; would have been more than suffcient
// for the task, but if stack is precious...
char *d = (char *) malloc(3);
for (i_m = 0; i_m < 5; i_m++) {
// it's always the same, just do it once at the beginning
//char *d = (char *) malloc(3);
strncpy(d, str, 2);
// atol() is for base 10 input only, use strtol() instead
n_oframe[i_m] = (int16_t)strtol(d,NULL,16);
str = str + 2;
//free(d);
}
for (j = 0; j < 5; j++) {
printf("%d ", n_oframe[j]);
}
putchar('\n');
free(d);
free(strp);
exit(EXIT_SUCCESS);
}
I changed gint16 to int16_t because I do not know what that is supposed to be. You can most likely replace it with gint16 without problems.
Related
I am having trouble with the very last line in my function, where I am stilly learning the basics of C. I have the signature of this function given and am tasked to write a function to concatenate two strings. The commented line outputs the correct result.
#include <stdio.h>
#include <stdlib.h>
// 1) len = dst-len + max_dst_len
int strlcat(char *dst, const char *src, int max_dst_len) {
int len = 0;
while (dst[len] != '\0') {
len++;
}
int total_len = len + max_dst_len;
char *new_str = malloc(sizeof(char) * total_len);
for (int i = 0; i < len; i++) {
new_str[i] = dst[i];
}
for (int i = len; i < total_len; i++) {
new_str[i] = src[i - len];
}
new_str[total_len] = '\0';
//printf("%s <--\n", new_str);
dst = *new_str;
return total_len;
}
int main() {
char test1[] = "dst";
char test1src[] = "src";
printf("%s\n", test1);
printf("%d\n", strlcat(test1, test1src, 10));
printf("%s\n", test1);
}
You should not be adding max_dst_len to the length of dst. max_dst_len is the amount of memory that's already allocated in dst, you need to ensure that the concatenated string doesn't exceed this length.
So you need to subtract len from max_dst_len, and also subtract 1 to allow room for the null byte. This will tell you the maximum number of bytes you can copy from src to the end of dst.
In your main() code, you need to declare test1 to be at least 10 bytes if you pass 10 as the max_dst_len argument. When you omit the size in the array declaration, it sizes the array just big enough to hold the string you use to initialize it. It's best to use sizeof test1 as this argument, to ensure that it's correct for the string you're concatenating to.
#include <stdio.h>
int strlcat(char *dst, const char *src, int max_dst_len) {
int len = 0;
while (dst[len] != '\0') {
len++;
}
int len_to_copy = max_dst_len - len - 1;
int i;
for (i = 0; i < len_to_copy && src[i] != '\0'; i++) {
dst[len+i] = src[i];
}
dst[i] = '\0';
//printf("%s <--\n", new_str);
return i + len;
}
int main() {
char test1[6] = "dst";
char test1src[] = "src";
printf("%s\n", test1);
printf("%d\n", strlcat(test1, test1src, sizeof test1));
printf("%s\n", test1);
}
static char* test_encrypt_ecb_verbose(char* plain_text_char, char* key_char)
{
uint8_t i,j, buf[64];
uint8_t plain_text[64];
uint8_t* outstr;
outstr = '\0';
memcpy(key,key_char,16) ;
memcpy(plain_text, plain_text_char, 64);
memset(buf, 0, 64);
printf("ECB encrypt verbose:\n\n");
printf("plain text:\n");
for(i = (uint8_t) 0; i < (uint8_t) 4; ++i)
{
phex(plain_text + i * (uint8_t) 16);
}
printf("\n");
printf("key:\n");
phex(key);
printf("\n");
// print the resulting cipher as 4 x 16 byte strings
printf("ciphertext:\n");
for(i = 0; i < 4; ++i)
{
AES128_ECB_encrypt(plain_text + (i*16), key, buf+(i*16));
phex(buf + (i*16));
//function to encrypt
}
printf("decryptedtext:\n");
for (i = 0; i < 4; ++i)
{
AES128_ECB_decrypt(buf + (i * 16), key, plain_text + (i * 16));
phex(plain_text + (i * 16));
//function to decrypt
}
//memcpy(outstr, buf, 64);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 16; j++)
{
outstr[j] = buf + (i * 16);
}
}
In the above code snippet I want to return the output array after encryption as string . Two of my attempts are there at the end. But those aren't correct. Can anyone suggest the correct way?
a char array and a char pointer is not the same thing.
If you need more details you should refer to this post
and it will gives you a solution to get a char * from a char array
char* p = &a[0];
a is your char array and p your destination pointer
then return your pointer. Using your code you can also directly use the char * you get as function parameters
To get it back you should add an additional argument:
static void test_encrypt_ecb_verbose(char* plain_text_char, char* key_char, char** cipher_text_char)
{
... your function ...
*cipher_text_char = malloc(64);
memcpy(*cipher_text_char, buf, 64);
}
From the caller you just do
char* cipher_text_char = NULL;
test_encrypt_ecb_verbose(plain_text_char, key_char, &cipher_text_char);
After test_encrypt_ecb_verbose has been executed, cipher_text_char will point to the memory allocated inside the function.
As an example consider this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* myfunc(char* src, char** dst, int len)
{
*dst = (char*)malloc(len);
memcpy(*dst, src, len);
return *dst;
}
int main(int argc, char* argv[])
{
char* src = "MyFuncTest";
char* dst = NULL;
char* p = NULL;
p = myfunc(src, &dst, strlen(src) + 1);
printf("dst = %s\n", dst);
printf("p = %s\n", p);
return 0;
}
The output is:
dst = MyFuncTest
p = MyFuncTest
How can I split a const char * string in the fastest possible way.
char *inputStr="abcde";
char buff[500];
I would like to have in buffer the following formatted string, format of which must be:
IN('a','ab','abc','abcd','abcde')
I'm learning C and new to the language. I have no clue where to start on this splitting problem.
I don't think you can do this particularly "fast", it seems like it's quite heavily limited since it needs to iterate over the source string many times.
I'd do something like:
void permute(char *out, const char *in)
{
const size_t in_len = strlen(in);
char *put;
strcpy(out, "IN(");
put = out + 3;
for(i = 1; i < in_len; ++i)
{
if(i > 1)
*put++ = ',';
*put++ = '\'';
memcpy(put, in, i);
put += i;
*put++ = '\'';
}
*put++ = ')';
*put++ = '\0';
}
Note that this doesn't protect against buffer overrun in the output.
You could use strcpy, strcat/strncat and a simple loop:
#include <stdio.h>
#include <string.h>
int main(void) {
char* inputStr = "abcde";
char buff[500];
// start the formatted string:
strcpy(buff,"IN(");
int i, len = strlen(inputStr);
for (i = 0; i < len; ++i) {
strcat(buff, "'");
strncat(buff, inputStr, i + 1);
strcat(buff, "'");
// if it is not last token:
if (i != len - 1)
strcat(buff, ",");
}
// end the formatted string:
strcat(buff,")");
printf("%s", buff);
return 0;
}
outputs the desired IN('a','ab','abc','abcd','abcde')
To give you a start, consider the following code:
char buffer[64];
const char str[] = "abcde";
for (size_t i = 1; i <= strlen(str); ++i)
{
strncpy(buffer, str, i);
buffer[i] = '\0'; /* Make sure string is terminated */
printf("i = %lu, buffer = \"%s\"\n", i, buffer);
}
The above code should print
i = 1, buffer = "a"
i = 2, buffer = "ab"
i = 3, buffer = "abc"
i = 4, buffer = "abcd"
i = 5, buffer = "abcde"
If you are looking for something like this in C++:-
#include <iostream>
#include <string.h>
using namespace std;
int main() {
const char *inputStr = "abcde"; //const to remove warning of deprecated conversion
char buff[500];
int count = 0;
for (int i = 0; i < (int) strlen(inputStr); i++) { //cast it to int to remove
// warning of comparison between signed and unsigned
for (int j = 0; j <= i; j++) {
buff[count++] = inputStr[j];
}
buff[count++] = ',';
}
buff[--count] = '\0';
cout << buff;
return 0;
}
Output - a,ab,abc,abcd,abcde
I know that getline is C++ standard but I need to read a line of digits:
123856
and save it to an array. But how to do this without spaces between given (as input) digits? I want a user input to be:
123856 (with no spaces) and then save it to an array (n element array) and after that, I want my array to look like this:
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 8;
array[4] = 5;
array[5] = 6;
But how to make it in C, without a getline?
This is NOT what I want:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char **argv)
{
int t[4];
int i;
for(i=0; i<4; i++)
scanf("%d", &t[i]);
for(i=0; i<4; i++)
printf("%d\n", t[i]);
return 0;
}
If I understood you correct, the following should do it:
read the whole line
loop through the string as long as you get digits or the string ends
for every digit, place it's value in your array and increase the index by 1
while( ( c = getchar()) != EOF && c != '\n' && i < max ) {
/* If desired, add check for value outside of 0-9 */
array[ i++ ] = c - '0';
...
}
char arr[] = "1234567";
int intarr[10];
int count = 0;
for (char* ptr = arr; *ptr; ptr++) {
intarr[count] = *ptr - '0';
count++;
}
try this
#include <stdio.h>
#include <string.h>
main (int argc, char *argv[])
{
FILE *f;
int i=0;
int j=0;
char output[100];
char* output1[100];
char string[100];
char delims1[] = " ";
char delims2[] = "*";
char* result = NULL;
char* result3 = NULL;
int num;
//for (j=0; j<2; j++)
//{
//printf("%s",delims9[6]);
//}
f = fopen("text.txt","r");
//
while( fgets(string,sizeof(string),f) )
{
result = strtok( string, delims1 );
while( result != NULL )
{
output1[i]=result;
printf("%s\n",output1[i]);
result = strtok( NULL, delims1 );
i++;
}
for (num = 0; num < 100; i++ ) //
{ // Error On this array
printf("%s\n", output1[i]); //
} //
}
printf("\n%d",i/3+1);
return 0 ;
}
Ok, without using any string.
int digits = 123856;
int numofdigits = 1 + floor(log10(digits));
int digits_arr[numofdigits];
int i;
for(i = numofdigits-1; i >= 0; i--) {
digits_arr[i] = (int)floor(digits / pow(10, i)) % 10;
}
Try the below link... Same question asked here and get solution....
convert an integer number into an array
char * convertNumberIntoArray(unsigned int number) {
unsigned int length = (int)(log10((float)number)) + 1;
char * arr = (char *) malloc(length * sizeof(char)), * curr = arr;
do {
*curr++ = number % 10;
number /= 10;
} while (number != 0);
return arr;
}
I am having an array of integer say int example[5] = {1,2,3,4,5}. Now I want to convert them into character array using C, not C++. How can I do it?
Depending on what you really want, there are several possible answers to this question:
int example[5] = {1,2,3,4,5};
char output[5];
int i;
Straight copy giving ASCII control characters 1 - 5
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i];
}
characters '1' - '5'
for (i = 0 ; i < 5 ; ++i)
{
output[i] = example[i] + '0';
}
strings representing 1 - 5.
char stringBuffer[20]; // Needs to be more than big enough to hold all the digits of an int
char* outputStrings[5];
for (i = 0 ; i < 5 ; ++i)
{
snprintf(stringBuffer, 20, "%d", example[i]);
// check for overrun omitted
outputStrings[i] = strdup(stringBuffer);
}
#include <stdio.h>
int main(void)
{
int i_array[5] = { 65, 66, 67, 68, 69 };
char* c_array[5];
int i = 0;
for (i; i < 5; i++)
{
//c[i] = itoa(array[i]); /* Windows */
/* Linux */
// allocate a big enough char to store an int (which is 4bytes, depending on your platform)
char c[sizeof(int)];
// copy int to char
snprintf(c, sizeof(int), "%d", i_array[i]); //copy those 4bytes
// allocate enough space on char* array to store this result
c_array[i] = malloc(sizeof(c));
strcpy(c_array[i], c); // copy to the array of results
printf("c[%d] = %s\n", i, c_array[i]); //print it
}
// loop again and release memory: free(c_array[i])
return 0;
}
Outputs:
c[0] = 65
c[1] = 66
c[2] = 67
c[3] = 68
c[4] = 69
You can convert a single digit-integer into the corresponding character using this expression:
int intDigit = 3;
char charDigit = '0' + intDigit; /* Sets charDigit to the character '3'. */
Note that this is only valid, of course, for single digits. Extrapolating the above to work against arrays should be straight-forward.
You need to create the array, because sizeof(int) is (almost surely) different from sizeof(char)==1.
Have a loop in which you do char_example[i] = example[i].
If what you want is to convert an integer into a string you could just sum your integer to '0' but only if you're sure that your integer is between 0 and 9, otherwise you'll need to use some more sophisticated like sprintf.
In pure C I would do it like this:
char** makeStrArr(const int* vals, const int nelems)
{
char** strarr = (char**)malloc(sizeof(char*) * nelems);
int i;
char buf[128];
for (i = 0; i < nelems; i++)
{
strarr[i] = (char*)malloc(sprintf(buf, "%d", vals[i]) + 1);
strcpy(strarr[i], buf);
}
return strarr;
}
void freeStrArr(char** strarr, int nelems)
{
int i = 0;
for (i = 0; i < nelems; i++) {
free(strarr[i]);
}
free(strarr);
}
void iarrtostrarrinc()
{
int i_array[] = { 65, 66, 67, 68, 69 };
char** strarr = makeStrArr(i_array, 5);
int i;
for (i = 0; i < 5; i++) {
printf("%s\n", strarr[i]);
}
freeStrArr(strarr, 5);
}