Byte Dataype in C

R

rahul

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
 
R

Ravi Uday

rahul said:
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
 
K

kalinga

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

dot

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?

Sure... just add

typedef unsigned char byte;

to the top of your code.

From there on you can use things like:

byte x, y;
byte MyArray[300];


As it turns out a C char is almost always 8 bits, just like a byte in other
compilers.
 
W

Walter Roberson

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

Keith Thompson

kalinga said:
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.
 
E

E. Robert Tisdale

A byte is a data *size* not a data *type*
in C or any other computer programming language.
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.
 
K

Keith Thompson

E. Robert Tisdale said:
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".
 
E

Eric Sosman

E. Robert Tisdale said:
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.)
 
M

Minti

Eric said:
A valid distinction.


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
 
M

Minti

Ravi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top