Converting raw Bytes to String

T

Tom

A have a set of bytes with values in a range from 128 - 255 (Each bit
of each byte represents some device status). This set of bytes needs
to be passed into an SNMP tool in which the input type is a string. For
bytes in the 0 -127 range, I was using:

string temp = Encoding.ASCII.GetString(BITResults, 0,
BITResults.Length);

But this does not work with values higher than 127.

Any help out there???

Tom
 
L

Larry Lard

Tom said:
A have a set of bytes with values in a range from 128 - 255 (Each bit
of each byte represents some device status). This set of bytes needs
to be passed into an SNMP tool in which the input type is a string. For
bytes in the 0 -127 range, I was using:

string temp = Encoding.ASCII.GetString(BITResults, 0,
BITResults.Length);

But this does not work with values higher than 127.

Although 'ASCII' is colloquially used to refer to characters as byte
values, in fact it is only a 7 bit encoding. Try Encoding.Default
instead, which refers to the system's current ANSI code page. It will
probably work.
 
T

Tom

Thanks Larry,

That seems to work... Almost. When viewing the values in the debugger,
it seems that the values for 7 of the 8 bytes are converted to the
proper values, while 1 byte ends up with some high value.

Here is the Byte array before encoding:

[0] 128 byte
[1] 144 byte
[2] 160 byte
[3] 176 byte
[4] 192 byte
[5] 208 byte
[6] 224 byte
[7] 242 byte

Here is the temp string after encoding:

temp[0] 8364 '€'
temp[1] 144 ''
temp[2] 160 ' '
temp[3] 176 '°'
temp[4] 192 'À'
temp[5] 208 'Ð'
temp[6] 224 'à'
temp[7] 242 'ò'


If anyone know why this might be happening, it would be interesting to
know.

Tom kuhn
 
L

Larry Lard

Tom said:
Thanks Larry,

That seems to work... Almost. When viewing the values in the debugger,
it seems that the values for 7 of the 8 bytes are converted to the
proper values, while 1 byte ends up with some high value.

Here is the Byte array before encoding:

[0] 128 byte [...]

Here is the temp string after encoding:

temp[0] 8364 '€' [...]

If anyone know why this might be happening, it would be interesting to
know.

This is why I don't like dealing with external components that demand
strings when they actually want bytes :( I suggest you wait for Jon
Skeet or similar to come along and tell me my answer was utterly wrong,
and what you should in fact be doing ...
 
C

Cor Ligthert [MVP]

Tom,

This is definitly Jons section, however maybe can this do it in advance.

byte a = 255;

string b = ((char)a).ToString();

I hope this helps,

Cor
 
T

Tom

I agree Larry,

I just wanted to add what the code looks like:

(Checked the BITResults, see previous post)

string temp = Encoding.Default.GetString(BITResults, 0,
BITResults.Length);

(Checked the String, See previous post)

Tom
 
T

Tom

Bravo!! This worked out great.

Here is an 8 bit ASCII encoder for anyone who needs one. I am not sure
if there is a more efficient solution, but this works.

public static string EightBitStringEncoder(byte[] array, int
nStartIndex, int nLength)
{
try
{
string sTemp = "";

for (int Count = 0 + nStartIndex; Count < nLength; Count
++)
{
sTemp += ((char)array[Count]).ToString();
}

return sTemp;
}
catch
{
return "";
}
 
J

Jon Skeet [C# MVP]

Tom said:
Here is an 8 bit ASCII encoder for anyone who needs one.

It's no such thing. It's an ISO-8859-1 encoder.

A better way to get an ISO-8859-1 encoder is:

Encoding.GetEncoding(28591);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top