473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting raw Bytes to String

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

Nov 17 '05 #1
7 7777

Tom wrote:
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.

--
Larry Lard
Replies to group please

Nov 17 '05 #2
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

Nov 17 '05 #3

Tom wrote:
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 ...

--
Larry Lard
Replies to group please

Nov 17 '05 #4
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

Nov 17 '05 #5
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
Nov 17 '05 #6
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 "";
}

Nov 17 '05 #7
Tom <to*****@cfl.rr.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
1
by: Erinys | last post by:
Hi, I need to access the individual bytes in a string in my javascript function. If the characters in the string were all ascii characters then there would not be a problem, however in my case...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
1
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.