By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

Byte Dataype in C

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a


rahul wrote:
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


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

Nov 14 '05 #2

P: n/a
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/DataOutputStream on the java side.
Ravi Uday wrote:
rahul wrote:
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
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


Nov 14 '05 #3

P: n/a
In article <0b********************************@4ax.com>, <do*@dot.dot> wrote:
As it turns out a C char is almost always 8 bits, just like a byte in other
compilers.


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

P: n/a
"kalinga" <ra******@yahoo.com> writes:
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/DataOutputStream on the java side.


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.programmer or comp.os.ms-windows.programmer.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 14 '05 #5

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

rahul wrote:
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.
No conversion is necessary.
A character occupies one byte of data.
How can I accomplish this in C?
I am using [GNU C].
Is there a way to have a byte datatype in C?


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.
Nov 14 '05 #6

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
rahul wrote:

[...]
How can I accomplish this in C?
I am using [GNU C].
Is there a way to have a byte datatype in C?


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


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 "addressable unit of data
storage large enough to hold any member of the basic character set of
the execution environment". Whether an "addressable 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_Keith) ks***@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.
Nov 14 '05 #7

P: n/a
E. Robert Tisdale wrote:
A byte is a data *size* not a data *type*
A valid distinction.
in C or any other computer programming language.


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
es*****@acm-dot-org.invalid
Nov 14 '05 #8

P: n/a

Eric Sosman wrote:
E. Robert Tisdale wrote:
A byte is a data *size* not a data *type*


A valid distinction.
in C or any other computer programming language.


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.)


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.com, 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

Nov 14 '05 #9

P: n/a

Ravi Uday wrote:
rahul wrote:
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


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


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.com, 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

Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.