Byte Dataype in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rahul

    #1

    Byte Dataype in C

    Hi!,

    I have a java program that receives data via sockets from a C program.
    The java program has to recieve the data in byte format. I need to
    send character arrays from the C side after converting data in byte
    arrays. How can I accomplish this in C? I am using ansi C(gnu). Is
    there a way to have a byte datatype in c?

    Thanks,
    Rahul
  • Ravi Uday

    #2
    Re: Byte Dataype in C



    rahul wrote:
    [color=blue]
    > Hi!,
    >
    > I have a java program that receives data via sockets from a C program.
    > The java program has to recieve the data in byte format. I need to
    > send character arrays from the C side after converting data in byte
    > arrays. How can I accomplish this in C? I am using ansi C(gnu). Is
    > there a way to have a byte datatype in c?
    >
    > Thanks,
    > Rahul[/color]

    You could use 'char' to represent a byte data.

    In C a -
    Byte means - the unit of data storage in the execution environment
    large enough to hold any member of the basic character set of the
    execution environment. It shall be possible to express the address
    of each individual byte of an object uniquely. A byte is composed of a
    contiguous sequence of bits, the number of which is
    implementation-defined. The least significant bit is called the
    low-order bit; the most significant bit is called the high-order bit.


    See if that is what you want :)

    - Ravi

    Comment

    • kalinga

      #3
      Re: Byte Dataype in C

      Hi Rahul!,

      I figure there is way to represent byte data in char but what I am
      wondering is that is there any library function in gnu c which I can
      use to send this byte data over a socket in such a way so that I can
      read it using DataInputStream/DataOutputStrea m on the java side.


      Ravi Uday wrote:[color=blue]
      > rahul wrote:
      >[color=green]
      > > Hi!,
      > >
      > > I have a java program that receives data via sockets from a C[/color][/color]
      program.[color=blue][color=green]
      > > The java program has to recieve the data in byte format. I need to
      > > send character arrays from the C side after converting data in byte
      > > arrays. How can I accomplish this in C? I am using ansi C(gnu). Is
      > > there a way to have a byte datatype in c?
      > >
      > > Thanks,
      > > Rahul[/color]
      >
      > You could use 'char' to represent a byte data.
      >
      > In C a -
      > Byte means - the unit of data storage in the execution environment
      > large enough to hold any member of the basic character set of the
      > execution environment. It shall be possible to express the[/color]
      address[color=blue]
      > of each individual byte of an object uniquely. A byte is[/color]
      composed of a[color=blue]
      > contiguous sequence of bits, the number of which is
      > implementation-defined. The least significant bit is called the
      > low-order bit; the most significant bit is called the high-order[/color]
      bit.[color=blue]
      >
      >
      > See if that is what you want :)
      >
      > - Ravi[/color]

      Comment

      • Walter Roberson

        #4
        Re: Byte Dataype in C

        In article <0bll411tg6k5u3 g2h8fksqe765v4e eatiu@4ax.com>, <dot@dot.dot> wrote:[color=blue]
        >As it turns out a C char is almost always 8 bits, just like a byte in other
        >compilers.[/color]

        A C character is pretty much defined to be one byte, with all other
        datatypes being defined in terms of occupying storage which is a multiple
        of the size of a character.

        On most machines, a C character is indeed 8 bits -- but not universally
        so. The cases in which it is -not- 8 bits would correspond to cases in
        which the machine's byte itself is not 8 bits -- which does happen,
        especially for embedded processors, and for some purpose-built
        processors aimed mostly a niche problems rather than general purpose
        computing. (For example, a processor designed for multimedia work
        might have a "byte" which is 48 bits wide, since such a processor
        would rarely have to do text manipulations.)
        --
        Ceci, ce n'est pas une idée.

        Comment

        • Keith Thompson

          #5
          Re: Byte Dataype in C

          "kalinga" <rahul_41@yahoo .com> writes:[color=blue]
          > I figure there is way to represent byte data in char but what I am
          > wondering is that is there any library function in gnu c which I can
          > use to send this byte data over a socket in such a way so that I can
          > read it using DataInputStream/DataOutputStrea m on the java side.[/color]

          Please don't top-post; your response belongs after any quoted text.

          Standard C doesn't define sockets. Try asking in a newsgroup specific to
          your OS, such as comp.unix.progr ammer or comp.os.ms-windows.program mer.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • E. Robert Tisdale

            #6
            Re: Byte Dataype in C

            A byte is a data *size* not a data *type*
            in C or any other computer programming language.

            rahul wrote:
            [color=blue]
            > I have a java program that receives data via sockets from a C program.
            > The java program [must] recieve the data in byte format.
            >
            > I need to send character arrays from the C side
            > after converting data in byte arrays.[/color]

            No conversion is necessary.
            A character occupies one byte of data.
            [color=blue]
            > How can I accomplish this in C?
            > I am using [GNU C].
            > Is there a way to have a byte datatype in C?[/color]

            No.
            Not in C or any other computer programming language.

            #include <sys/types.h>
            #include <sys/socket.h>


            int send(int s, const void *msg, size_t len, int flags);

            Function send(int, const void*, size_t, int)
            does *not* specify a message (msg) of type byte.
            The length (len) is the number of bytes (characters)
            that you are sending.

            Comment

            • Keith Thompson

              #7
              Re: Byte Dataype in C

              "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:[color=blue]
              > rahul wrote:[/color]
              [...][color=blue][color=green]
              >> How can I accomplish this in C?
              >> I am using [GNU C].
              >> Is there a way to have a byte datatype in C?[/color]
              >
              > No.
              > Not in C or any other computer programming language.[/color]

              ERT is posting nonsense again.

              C does not have a built-in type called "byte", but it's perfectly
              reasonable to declare one of your own:

              typedef unsigned char byte;

              C overloads the char types (char, unsigned char, signed char) as both
              character types, representing text and control characters used for
              input/output, and as the fundamental unit of storage. This is, in my
              opinion, a design flaw in the language, but we're stuck with it. A
              typedef like the above can be a useful way to avoid the ambiguity.

              Or you can just use "unsigned char" directly, without the typedef, and
              keep in mind that the name "char" doesn't necessarily imply textual
              data.

              The C99 standard defines a "byte" as an "addressabl e unit of data
              storage large enough to hold any member of the basic character set of
              the execution environment". Whether an "addressabl e unit of data
              storage" is better referred to as a type or as a size is another
              question, but not a particularly interesting one IMHO.

              Other languages are off-topic here, but at least one language (Java)
              does define an intrinsic type called "byte".

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Eric Sosman

                #8
                Re: Byte Dataype in C

                E. Robert Tisdale wrote:
                [color=blue]
                > A byte is a data *size* not a data *type*[/color]

                A valid distinction.
                [color=blue]
                > in C or any other computer programming language.[/color]

                This, however, is nonsense and easily proven as
                such. One brief glance at Java will give it the lie --
                although Java commits the stupidity of making `byte'
                a signed type. (Damn it all! They got `char' right,
                as C did not, but they botched `byte' ... Geniuses are
                the cross the rest of us bear, for lack of, er, genius.)

                --
                Eric Sosman
                esosman@acm-dot-org.invalid

                Comment

                • Minti

                  #9
                  Re: Byte Dataype in C


                  Eric Sosman wrote:[color=blue]
                  > E. Robert Tisdale wrote:
                  >[color=green]
                  > > A byte is a data *size* not a data *type*[/color]
                  >
                  > A valid distinction.
                  >[color=green]
                  > > in C or any other computer programming language.[/color]
                  >
                  > This, however, is nonsense and easily proven as
                  > such. One brief glance at Java will give it the lie --
                  > although Java commits the stupidity of making `byte'
                  > a signed type. (Damn it all! They got `char' right,
                  > as C did not, but they botched `byte' ... Geniuses are
                  > the cross the rest of us bear, for lack of, er, genius.)[/color]

                  When I was learning Java, I had hard time figurinjg out WTH does it not
                  have any unsigned types. The only _reason_ people pointed out to me was
                  that "Oak did not have it, so java does not have". Damn it why did Oak
                  did not have it. Unsigned int's are I believe quite necessary in
                  several domains.

                  --
                  Imanpreet Singh Arora
                  "If you want to post a followup via groups.google.c om, don't use
                  the broken "Reply" link at the bottom of the article. Click on
                  "show options" at the top of the article, then click on the
                  "Reply" at the bottom of the article headers." - Keith Thompson

                  Comment

                  • Minti

                    #10
                    Re: Byte Dataype in C


                    Ravi Uday wrote:[color=blue]
                    > rahul wrote:
                    >[color=green]
                    > > Hi!,
                    > >
                    > > I have a java program that receives data via sockets from a C[/color][/color]
                    program.[color=blue][color=green]
                    > > The java program has to recieve the data in byte format. I need to
                    > > send character arrays from the C side after converting data in byte
                    > > arrays. How can I accomplish this in C? I am using ansi C(gnu). Is
                    > > there a way to have a byte datatype in c?
                    > >
                    > > Thanks,
                    > > Rahul[/color]
                    >
                    > You could use 'char' to represent a byte data.[/color]

                    As Eric has pointed out, you need to specifically use "signed char".

                    --
                    Imanpreet Singh Arora
                    "If you want to post a followup via groups.google.c om, don't use
                    the broken "Reply" link at the bottom of the article. Click on
                    "show options" at the top of the article, then click on the
                    "Reply" at the bottom of the article headers." - Keith Thompson

                    Comment

                    Working...