0

So I'm trying to make a string to binary method in C. Here is my code so far:

int main(int argc, char **argv) {
    convertStringToBits("hello");
}

char *convertStringToBits(char **string) {
    int i;
    int stringLength = strlen(string);
    int mask = 0x80; /* 10000000 */
    char charArray[stringLength * 8]; //* 8 for char bits?

    for(i = 0; i < (sizeof(string) / sizeof(char)) + 1; i++) {
        mask = 0x80;
        char c = string[i];
        while(mask > 0) {
            printf("%d", (c & mask) > 0);
            mask >>= 1; /* move the bit down */
            //add to char array
        }
        printf("\n");
    }

    return charArray;
}

The expected output of: hello should be:

01101000
01100101
01101100
01101100
01101111

But i get this:

01101000
01101111
01100011
00100000
01011011

I also want to return an array of characters but I can't seem to do this.

Thanks for the help!

11
  • 2
    sizeof(char) is always 1. Commented Jan 24, 2016 at 12:20
  • @iharob not on all systems I don't think (e.g. Raspbian) Commented Jan 24, 2016 at 12:21
  • 2
    It's mandatory by the standard. It MUST be 1. Whether CHAR_BITS is 8 or not, that's something else but sizeof(char) MUST be 1. Commented Jan 24, 2016 at 12:22
  • int stringLength = (sizeof(string) / sizeof(char)) + 1; - this will not work as expected Commented Jan 24, 2016 at 12:23
  • 1
    Do you have a compiler that doesn't emit warnings or did you ignore them? Commented Jan 24, 2016 at 12:34

2 Answers 2

4

Your calculation of stringLength is wrong, the sizeof is an operator and it gives you the size of a type, objects having array type will of course return the size of the array. But your string variable is not an array it's a pointer, and the size of a poitner is not the length of the contents it points to.

Also, I suspect you are doing something else wrong because this

char c = string[i];

is wrong, the type of string[i] is char * not char.

You didn't post all the code, but those are 2 mistakes in your code. It's not clear why you are passing char ** to the function if you don't want to alter the parameter.

Sign up to request clarification or add additional context in comments.

5 Comments

It gives me the amount of characters in the string so it is definitely working.
It's wrong. Don't contradict a more experienced programmer unless you have evidence that they're wrong. If it's correct, why are you adding 1? and what is the length of the string? Can you change it?
So how would I calculate stringLength properly then?
strlen - Look it up. sizeof is fixed at compile time
I don't know, you didn't post the code where you pass the string to the function. In principle it should be strlen(*string).
3

Oopsie:

 char **string

You want to pass a string, that is a char *. You're instead passing a pointer to a pointer.

This

int stringLength = (sizeof(string) / sizeof(char)) + 1;

appears to be working by chance, because sizeof(string) returns 4 (the size of a pointer) on your platform, and "Hello" is 5 characters. You want

int stringLength = strlen(string);

This also will not work:

char charArray[stringLength * 8]; //* 8 for char bits?

because its memory only "lives" inside the function. To pass it outside, you need to allocate it using malloc():

char *charArray;
charArray = malloc(8*stringLength+1);
// Check that malloc returned a valid pointer. It's almost certain
// that it will, but the one time it might return NULL...
if (charArray == NULL) {
    // ...you want to know it. Using charArray now would crash.
    printf("An error occurred\n");

    // Either return something harmless or something that will
    // show to the upstream code that an error occurred.
    return NULL;
}

12 Comments

Terrible NULL == charArray I can't even look at it. And why don't you use CHAR_BITS instead? Also, please read the code carefully.
@iharob - It is not terrible - it is yoda convention.
@EdHeal I know, I hate it so much. It's hard for me to understand. I know that it prevents accidental assignment, but it's unnatural to read.
Yes, sorry. And I'm updating the check. Is Yoda convention in any way official? I love it :-)
A matter of taste it is, free are people choose to their preferences. Talking reverse in to understand however diffcult is. But don't change it, it's really just a matter of preference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.