0

I have a piece of hardware which sends data via bluetooth. I'm trying to read them, but the info doesn't correspond to the original data.

I'm using ReadByte to receive the data which are sent one by one.

int i1 = 0;
byte[] datum = new byte[6144];

while (i1 < 6144)
{
   datum[i1] = await Task.Run(() => (byte)InmStream.ReadByte());
   i1++;
}


string string_ascii = (System.Text.Encoding.ASCII.GetString(datum));
string string_bit = BitConverter.ToString(datum);
string string_base64 = Convert.ToBase64String(datum);

Results:

If I display the elements from the strings created, I get:

string_ascii[0] and [3] = 3 and 3 ([1] and [2] are \r & \n)

string_bit[0] and [3] = 3 and 0 ([1] and [2] are \r & \n)

string_base64[0] and [3] = M and K ([1] and [2] are \r & \n)

Should be always: 3 and 35 (Data format sent from hardware: integer\r\n)

Obsviously if I try to convert the same elements to integers, the results won't be what I need.

ascii[i] = (int)Char.GetNumericValue(string_ascii[i]);
bit[i] = (int)Char.GetNumericValue(string_bit[i]);
base64[i] = (int)Char.GetNumericValue(string_base64[i]);

I get:

ascii[0] and [3] = 3 and 3

bit[0] and [3] = 3 and 0

base64[0] and [3] = -1 and -1

PS. I noticed that when there are 1 digit integers, the code works fine.

How do I fix this ? Thanks in advance.

dovid
  • 5,945
  • 3
  • 31
  • 66
Dias
  • 19
  • 1
  • 1
    Regarding the `3` and `35` you are expecting, are these string values or raw byte values. For example, is the first one `0x03` or `0x33`? If it's the string value, then your first `string_ascii` looks fine since characters 0 and 3 are in fact `3`. If you also check character 4, you might find a `5` there (second character of `35`). – Richard Pickett Jun 28 '17 at 22:34
  • Have a post about this.. https://stackoverflow.com/questions/1003275/how-to-convert-byte-to-string – Bruno Armando Jun 28 '17 at 22:39
  • 2
    Your various attempts at conversion are very confusing. Only the ASCII conversion seems even halfway reasonable, and then only if you know that the data is in fact ASCII (which it might be...refer to the first comment above). `BitConverter` gives you a hexadecimal representation of the bytes; if you are really passing the same bytes to the ASCII conversion as to `BitConverter`, the `3` and `0` make sense, but there's no way you should be getting `\r` and `\n` in there. And Base64? Whatever makes you think that would be a useful way to interpret the bytes? – Peter Duniho Jun 28 '17 at 23:54
  • 1
    In any case, without a good [mcve] that reliably reproduces your problem, you're not going to see anything except guesses. At the very least, you need to show the actual bytes you receive. I think the first comment above probably answers your question, but there's just not enough information in the question to know for sure. – Peter Duniho Jun 28 '17 at 23:55
  • @RichardPickett, straight to the point with no judgments concerning silly questions or similar things. Thanks for that. So, you were right, I should have looked for more data on the sequence, the 5 was just there and I missed the whole picture. Thanks for the hint. – Dias Jun 29 '17 at 15:50

0 Answers0