-2

How can I trim a string into pieces of N numbers of characters and then pass them as an array of strings into a function?

This in a part of my program that converts binary<->hex.

I tried doing the same thing with strings but it did not work.

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

#define MAXDIGITS 8  // 8bits 


int main()
{
    int y;

    printf("Binary-Hex convertor\n");
    printf("Enter the  Binary value : ");
    scanf("%d", &y);

    int i = MAXDIGITS - 1;
    int array[MAXDIGITS];

    while(y > 0)
    {
        array[i--] = y % 10;
        y /= 10;
    }

    printf("%s", "-----------------\n");
    printf("%s", "HEX:");

    int x = array[0];
    int x1 = array[1];
    int x2 = array[2];
    int x3 = array[3];
    int x4 = array[4];
    int x5 = array[5];
    int x6 = array[6];
    int x7 = array[7];

    char buffer[50];
    char buffer2[50];
    char buffer3[50];
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135

2 Answers2

0

Do you have to null terminate the strings, do you have a limit on this memory used. Do you need to allocate the memory correctly etc? A bit more info would be useful

const char *input_string = "HELLO THIS IS SOME INPUT STRING";
int N = 4; // The number to split on

// Work out how many strings we will end up in
int number_of_strings = (strlen(input_string) + (N - 1)) / N;

// ALlow for an extra string if you want to null terminate the list
int memory_needed = ((number_of_strings + 1) * sizeof(char *)) + (number_of_strings * (N + 1));
char *buffer = malloc(memory_needed);
char **pointers = (char **)buffer;
char *string_start = (buffer + ((number_of_strings + 1) * sizeof(char *));
int count = 0;

while ( *input_string != '\0' )
{
    // Fresh string
    if ( count == 0 )
    {
        *pointers++ = string_start;
        *pointers = NULL;  // Lazy null terminate
    }

    // Copy a character
    *string_start++ = *input_string++;
    *string_start = '\0';  // Again lazy terminat     

    count++;

    if ( count == N )
    {
        count = 0;
        string_start++; // Move past the null terminated string
    }
}

You can then pass (char **)buffer; to a routine. I havent actually tried this, ive been lazy in the terminating of the strings. You could just terminate at the end of a count run and the end of the while loop. This isnt exactly pretty code, but it should do the job. Bit more info on the other requirements might be good.

Adrian Brown
  • 456
  • 3
  • 14
0

If its just binary to hex from a string then this is much easier....

char *input_string = "1001010101001010";
int count = 0;
int value = 0;

while ( *input_string != '\0' )
{
    // Might be worth checking for only 0 and 1 in input string
    value <<= 1;
    value |= (int)((*input_string--) - '0');

    if ( ++count == 8 || *input_string == '\0' )
    {
        // USE value to print etc, if you want to display use 
        // the following else you could store this in an array etc.
        printf("%x ", value);
        count = 0;
        value = 0;
    }
}
Adrian Brown
  • 456
  • 3
  • 14