0

I have a chunk of binary data which contains structures with offsets and then strings; in C++ it is easy:

struct foo
{
 int offset;
 char * s;
}

void * data; 
... data is read and set
foo * header = (foo*) data;
header->s = (int)header-> + (int)data;
int len = strlen(header->s);
char* ns = new char[len+1];
strcpy(ns,header->s);

simple enough... in C# how would you do this? The biggest problem is that I don't know the length of the string. It is null terminated.

I have the data in a byte[] and an IntPtr to the memory but I need a POINTER to that data a a string (char *) something that I can get the length of the string.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vbbartlett
  • 179
  • 2
  • 14
  • 1
    Is it *actually* text data? Or is it just arbitrary binary data, e.g. encrypted data or an image? – Jon Skeet Jun 28 '12 at 16:11
  • Why doesn't the C++ code just use `std::string`? Does it need to interoperate with a C library? – dan04 Jun 28 '12 at 16:13
  • 1
    dan04, the c++ is just posted as an example of how he knows it can be done, but he needs that done in C#. --someone correct me if I'm wrong about that. – hometoast Jun 28 '12 at 16:15
  • Just convert the byte[] array to a string there is a method for that. There also is an app for it I suppose. – Security Hound Jun 28 '12 at 16:18

1 Answers1

-1

C# is a high level language, and working with pointers is simply unnatural for this language.

To convert the data from byte array to a string, you can use the BitConverter class:

BitConverter.ToInt32(byte_array, start index);

To convert it to a string, you can use the StringBuilder class:

StringBuilder str = new StringBuilder();
// i=starting index of text
for (int i = 3; i<byte_array.Length; i++)
  str.Append(byte_array[i];

return str.ToString();

If there is more data after the string, you can put the stopping condition for the loop byte_array[i]!=0, and when it stops, byte_array[i] will be the string terminator. Save the value of i, and you can get the data after it.

Another method of doing this is to use the ASCIIEncoding.ASCII.GetString() method:

ASCIIEncoding.ASCII.GetString(byte_array, start_index, bytes_count);
Tibi
  • 4,015
  • 8
  • 40
  • 64
  • I could use the stringbuilder class but then looping through each character in the array to see if it is null seems a bit tedious. as for the asciiencoding, will that work with data that is not ascii? The reason I ask is that the string has some data encoding hidden in it. ie if it has a char 0x80, then the next character is a special processed character. It is a custom format Im parsing so i can't change it. (Porting old data to a different endian machine). – vbbartlett Jun 28 '12 at 20:15
  • In c++, the char type stores ASCII characters, and since you didn't specify any other encoding... Well, in that case you need to find out what encoding you have. I found that the Encoding class has static properties for ASCII, Unicode, UTF32, UTF8, UTF7, BigEndianUnicode. If it's one of these, they all have a GetString() function. – Tibi Jun 29 '12 at 05:56
  • @vbbartlett 1. Do not use BitConverter for strings; 2. As I understand, you can specify any suitable encoding (which supports your characters, I think UTF8 should be ok), just make sure it's the same on both sides (GetBytes and GetString). It's just the encoding in which your string will be stored in byte_array. – Vlad Dec 26 '15 at 21:06