Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte[] to ASCII

I received the contents of a text file returned in binary values:

Byte[] buf = new Byte[size];
stream = File.InputStream;
stream.Read(buf, 0, size);

How can I convert this to ASCII?

like image 378
The Mask Avatar asked Jul 02 '11 03:07

The Mask


People also ask

How many bytes is an ASCII character?

An ASCII character in 8-bit ASCII encoding is 8 bits (1 byte), though it can fit in 7 bits. An ISO-8895-1 character in ISO-8859-1 encoding is 8 bits (1 byte).

How do you do ASCII?

To insert an ASCII character, press and hold down ALT while typing the character code. For example, to insert the degree (º) symbol, press and hold down ALT while typing 0176 on the numeric keypad. You must use the numeric keypad to type the numbers, and not the keyboard.

How do you convert hex to bytes?

To convert hex string to byte array, you need to first get the length of the given string and include it while creating a new byte array. byte[] val = new byte[str. length() / 2]; Now, take a for loop until the length of the byte array.


5 Answers

You can use:

System.Text.Encoding.ASCII.GetString(buf); 

But sometimes you will get a weird number instead of the string you want. In that case, your original string may have some hexadecimal character when you see it. If it's the case, you may want to try this:

System.Text.Encoding.UTF8.GetString(buf); 

Or as a last resort:

System.Text.Encoding.Default.GetString(bytearray); 
like image 38
Patrick Desjardins Avatar answered Sep 23 '22 11:09

Patrick Desjardins


Use:

System.Text.Encoding.ASCII.GetString(buf);

like image 147
Jalal Said Avatar answered Sep 20 '22 11:09

Jalal Said


Encoding.ASCII.GetString(buf);
like image 41
Mrchief Avatar answered Sep 19 '22 11:09

Mrchief


As an alternative to reading a data from a stream to a byte array, you could let the framework handle everything and just use a StreamReader set up with an ASCII encoding to read in the string. That way you don't need to worry about getting the appropriate buffer size or larger data sizes.

using (var reader = new StreamReader(stream, Encoding.ASCII))
{
    string theString = reader.ReadToEnd();
    // do something with theString
}
like image 38
Jeff Mercado Avatar answered Sep 21 '22 11:09

Jeff Mercado


Encoding.GetString Method (Byte[]) convert bytes to a string.

When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

Syntax

public virtual string GetString(byte[] bytes)

Parameters

bytes
    Type: System.Byte[]
    The byte array containing the sequence of bytes to decode.

Return Value

Type: System.String
A String containing the results of decoding the specified sequence of bytes.

Exceptions

ArgumentException        - The byte array contains invalid Unicode code points.
ArgumentNullException    - bytes is null.
DecoderFallbackException - A fallback occurred (see Character Encoding in the .NET Framework for complete explanation) or DecoderFallback is set to DecoderExceptionFallback.

Remarks

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.

See the Remarks under Encoding.GetChars for more discussion of decoding techniques and considerations.

like image 34
BrunoLM Avatar answered Sep 21 '22 11:09

BrunoLM