473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Nov 14 '05 #1
9 5792


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
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
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
"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
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
"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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
1
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
by: anj2k5 | last post by:
I uploaded music file in SQl DB in a image dataype .But when i try to reterive it,It doesn't play. It gives the following type casting error on runtime "Unable to cast object of type...
15
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, This is being called in a C# loop within an ado.net transaction from C# 1.1 code. It is used to write large file data to a SQL Server database in chunks, rather than in one go. I had the...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.