Jump to content

Binary Data

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I've been working with binary data and null bytes have really been screwing me up because I can't do strlen on them.

1. What is the best way to store binary data in C? As a char*?

2. How can you get the "length" of the binary data if it contains null bytes (in C)?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'd handle it using a struct, where you use an integer to store the length and the char array. Basically, I'd steal from the C++ string implementation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
But how would I get the length?

#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Like winged said, you use a variable to control the length of the data, as you use and you see in container classes. What exactly are you trying to do with your working with binary data? If you're working with structures maybe you should use sizeof operator to return the size of a structure.

#5
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
I think you should use a char array, and use sizeof to determine the size. Take a look at this example code:
#include <stdio.h>

int main()
{
	char bin[]={1,0,1,0,1,1,0,1};
	printf("%d\n", sizeof(bin));
	return 0;
}
It stores some binary data in the bin array, and prints out how many bits it is.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Guest, in that situation it works, but when I pass bin to a function, it returns the sizeof the pointer.

#include <stdio.h>

void test(char *bin)
{
    printf("%d\n", sizeof(bin));

}

int main()
{
    char bin[]={1,0,1,0,1,1,0,1};
    test(bin);
    return 0;
}


#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
isn't it then you do

test(&bin);

or have I lost all memories from the pointers, references and stuff from C?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
When you read in data from a binary source, you should be using fread() already. If you do it like this:
size_t length = fread(myCharArray, 1, MY_CHAR_ARRAY_SIZE, readFile);
You can save length in a struct to represent the size of the array being passed. That's the best I can come up with. :P Sorry I couldn't be any more helpful than WingedPanther already has.
Wow I changed my sig!

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Basically, as you add/remove data from your char array, you'll need to be also updating your length variable. Everything you do will have to be synchronized.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog