why would one choose using byte[] instead of char[] or vica versa

Recommended Answers

All 2 Replies

byte is an unsigned 8-bit integer whose values range from 0 to 255.

char is a 16-bit unicode character type. If you want to represent raw streams of bytes, you'll use bytes. You could also parse the stream of bytes by assuming it uses some sort of encoding, like UTF-8 or ASCII or something, outputting a stream of chars. A char[] is basically a UTF-16 encoded array -- not all unicode characters can fit in one char -- some take two.

Edit: I'm kind of extrapolating when it comes to my statement that a char[] is supposed to be a UTF-16 encoded array. I would expect people to use string instead of char[], and I know I read somewhere that string is encoded in UTF-16.

thanks, i understood it, i forgot that c# represent char with two bytes unlike c/c++.

default.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

default.aspx.cs :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        double aDoubl = 0.1111111111111111111;
        float aSingl = 0.1111111111111111111F;
        long aLong = 1111111111111111111;
        int anInt = 1111111111;
        short aShort = 11111;
        char aChar = '*';
        bool aBool = true;

       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aDoubl)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aSingl)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aLong)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(anInt)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aShort)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aChar)) + "<br />");
       Response.Write(BitConverter.ToString(BitConverter.GetBytes(aBool)) + "<br />");
    }
}

good example for understanding representation of types.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.