473,326 Members | 2,148 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,326 software developers and data experts.

HELP PLEASE - How to convert string to byte array

Could some C guru help me please? A byte is an unsigned char in C.
How do I convert from a C string to a corresponding byte array. Any
help would be greatly appreciated.

Nov 14 '05 #1
17 14796
cp**********@yahoo.com scribbled the following:
Could some C guru help me please? A byte is an unsigned char in C.
How do I convert from a C string to a corresponding byte array. Any
help would be greatly appreciated.


You don't need to "convert" it. A C string can be used like a byte
array.

For example:
char *s = "Hello, world!";
char c = s[0]; /* c now contains 'H' */

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #2
cp**********@yahoo.com wrote:
Could some C guru help me please? A byte is an unsigned char in C.
No, 'byte' is a synonym for 'char', which may be either signed or
unsigned. An unsigned byte is an unsigned char. Whether there is any
type that corresponds to, for example, an octet depends upon the
implementation.
How do I convert from a C string to a corresponding byte array. Any
help would be greatly appreciated.


A C string *is* a zero-terminated byte array.

char foo[] = "a string";

foo is a string and a char (or byte) array containing
{'a', ' ', 's', 't', 'r', 'i', 'n', 'g', 0}

Nov 14 '05 #3
Martin Ambuhl wrote:
cp**********@yahoo.com wrote:
Could some C guru help me please? A byte is an unsigned char in C.


No, 'byte' is a synonym for 'char', which may be either signed or
unsigned. An unsigned byte is an unsigned char. Whether there is any
type that corresponds to, for example, an octet depends upon the
implementation.


What? Have I been away too long? I thought that a char is an entity
of value, but not necessarily one that resembles a byte. Maybe
I've missed a standard... :D

-atl-
--
A multiverse is figments of its own creations
Nov 14 '05 #4
Ari Lukumies wrote:
Martin Ambuhl wrote:
cp**********@yahoo.com wrote:
Could some C guru help me please? A byte is an unsigned char in C.

No, 'byte' is a synonym for 'char', which may be either signed or
unsigned. An unsigned byte is an unsigned char. Whether there is any
type that corresponds to, for example, an octet depends upon the
implementation.

What? Have I been away too long? I thought that a char is an entity
of value, but not necessarily one that resembles a byte. Maybe
I've missed a standard... :D


I have no idea what you think you said. A byte and a char are the same
by definition in C, and has been for 16 years, so you've missed not *a*
standard, but *every* standard.

Here's what a char is (3.1.2.5 in C89)
An object declared as type char is large enough to store any member
of the basic execution character set. If a member of the required
source character set enumerated in $2.2.1 is stored in a char object,
its value is guaranteed to be positive. If other quantities are
stored in a char object, the behavior is implementation-defined: the
values are treated as either signed or nonnegative integers.

Here's what a byte is (1.6 in C89)
* Byte --- 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.

and if that's not clear, note from the language for sizeof (3.3.3.4);
The sizeof operator yields the size (in bytes) of its operand, ...
When applied to an operand that has type char , unsigned char , or
signed char , (or a qualified version thereof) the result is 1
Nov 14 '05 #5
Ari Lukumies wrote:

Martin Ambuhl wrote:
cp**********@yahoo.com wrote:
Could some C guru help me please? A byte is an unsigned char in C.


No, 'byte' is a synonym for 'char', which may be either signed or
unsigned. An unsigned byte is an unsigned char.
Whether there is any
type that corresponds to, for example, an octet depends upon the
implementation.


What? Have I been away too long? I thought that a char is an entity
of value, but not necessarily one that resembles a byte. Maybe
I've missed a standard... :D


A byte is a unit of memory.
char is an object type.

--
pete
Nov 14 '05 #6
pete <pf*****@mindspring.com> scribbled the following:
Ari Lukumies wrote:
Martin Ambuhl wrote:
> cp**********@yahoo.com wrote:
>> Could some C guru help me please? A byte is an unsigned char in C.
>
> No, 'byte' is a synonym for 'char', which may be either signed or
> unsigned. An unsigned byte is an unsigned char.
> Whether there is any
> type that corresponds to, for example, an octet depends upon the
> implementation.
What? Have I been away too long? I thought that a char is an entity
of value, but not necessarily one that resembles a byte. Maybe
I've missed a standard... :D

A byte is a unit of memory.
char is an object type.


In the context of the C programming language, "byte" and "char" are the
same thing. In other contexts they might be different.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 14 '05 #7
Joona I Palaste wrote:
pete <pf*****@mindspring.com> scribbled the following:
Ari Lukumies wrote:
Martin Ambuhl wrote:

cp**********@yahoo.com wrote:

>Could some C guru help me please? A byte is an unsigned char in C.

No, 'byte' is a synonym for 'char', which may be either signed or
unsigned. An unsigned byte is an unsigned char.
Whether there is any
type that corresponds to, for example, an octet depends upon the
implementation.

What? Have I been away too long? I thought that a char is an entity
of value, but not necessarily one that resembles a byte. Maybe
I've missed a standard... :D


A byte is a unit of memory.
char is an object type.

In the context of the C programming language, "byte" and "char" are the
same thing. In other contexts they might be different.


I agree. Now if we can get Chris Torek to agree ..

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8
Joe Wright <jo********@comcast.net> wrote:
Joona I Palaste wrote:
pete <pf*****@mindspring.com> scribbled the following:
A byte is a unit of memory.
char is an object type.


In the context of the C programming language, "byte" and "char" are the
same thing. In other contexts they might be different.


I agree. Now if we can get Chris Torek to agree ..


Well, if you're being picky, pete is right: char is a basic object type;
a byte is the amount of memory that one object of type char takes.

Richard
Nov 14 '05 #9
Richard Bos wrote:
Joe Wright <jo********@comcast.net> wrote:

Joona I Palaste wrote:
pete <pf*****@mindspring.com> scribbled the following:
A byte is a unit of memory.
char is an object type.

In the context of the C programming language, "byte" and "char" are the
same thing. In other contexts they might be different.


I agree. Now if we can get Chris Torek to agree ..

Well, if you're being picky, pete is right: char is a basic object type;
a byte is the amount of memory that one object of type char takes.

Richard


Maybe a difference without a distinction. The term char is a C language
thing and the term byte is more generic, having to do with memory size
and disk size, etc. For most practical purposes the C char type
describes a byte object in memory and on disk. Some might suggest a
difference between a char object and a byte object. I am not one of them.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10
On Mon, 06 Jun 2005 19:28:04 -0400, Joe Wright wrote:
Richard Bos wrote:
Joe Wright <jo********@comcast.net> wrote:

Joona I Palaste wrote:

pete <pf*****@mindspring.com> scribbled the following:
>A byte is a unit of memory.
>char is an object type.

In the context of the C programming language, "byte" and "char" are the
same thing. In other contexts they might be different.

I agree. Now if we can get Chris Torek to agree ..

Well, if you're being picky, pete is right: char is a basic object type;
a byte is the amount of memory that one object of type char takes.

Richard


Maybe a difference without a distinction. The term char is a C language
thing and the term byte is more generic, having to do with memory size
and disk size, etc. For most practical purposes the C char type
describes a byte object in memory and on disk. Some might suggest a
difference between a char object and a byte object. I am not one of them.


As the standard defines them char is a type and byte is a typeless unit of
memory allocation. Although strongly related (character types are defined
to have a size of 1 byte) they are different concepts.

Note that in the description above there is no such thing in C as a "byte
object" (objects always have a type), a char object is a byte *sized*
object.

Lawrence

Nov 14 '05 #11
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.


An allocated object doesn't always have a type.

--
pete
Nov 14 '05 #12
On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.


An allocated object doesn't always have a type.


Can you provide an example?
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #13
Mark McIntyre <ma**********@spamcop.net> writes:
On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
An allocated object doesn't always have a type.


Can you provide an example?


The object that a non-null pointer returned by malloc() points to
initially has no type.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 14 '05 #14
Mark McIntyre wrote:

On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.


An allocated object doesn't always have a type.


Can you provide an example?


/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
void *pointer;

pointer = malloc(1);
printf("If %p", pointer);
puts(
" isn't a null pointer, then it's the address of an\n"
"allocated object that doesn't have a type."
);
free(pointer);
return 0;
}

/* END new.c */

--
pete
Nov 14 '05 #15
On Sat, 11 Jun 2005 23:44:45 +0000, pete wrote:
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.


An allocated object doesn't always have a type.


An allocated object is a grey area. The definition of the term "object"
says it is a region of data storage then contents of which can represent
values. Well a value in an object is an interpretation of a bit pattern
according to the rules of a type - if you have no type you can't represent
values.

Lawrence

Nov 14 '05 #16
Lawrence Kirby wrote:

On Sat, 11 Jun 2005 23:44:45 +0000, pete wrote:
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.
An allocated object doesn't always have a type.


An allocated object is a grey area.


Not really.
In C99 there's 3 kinds of durations for objects,
and allocated is one of them.
The definition of the term "object"
says it is a region of data storage then contents of which
can represent values.
It can represent values,
as long as you do what takes to get the values in there.

An uninitialised object of type int,
can't represent values either,
but it's still an object.
Well a value in an object is
an interpretation of a bit pattern
according to the rules of a type
- if you have no type you can't represent values.


*Until* you have a type, you can't represent values.

malloc returns the address of an object with indeterminate value.

--
pete
Nov 14 '05 #17
Lawrence Kirby <lk****@netactive.co.uk> writes:
On Sat, 11 Jun 2005 23:44:45 +0000, pete wrote:
Lawrence Kirby wrote:
Note that in the description above there is no
such thing in C as a "byte
object" (objects always have a type),
a char object is a byte *sized* object.


An allocated object doesn't always have a type.


An allocated object is a grey area. The definition of the term "object"
says it is a region of data storage then contents of which can represent
values. Well a value in an object is an interpretation of a bit pattern
according to the rules of a type - if you have no type you can't represent
values.


Any object that is designated has a type, which is the type of the
expression used to designate it; section 6.3.2.1 p1.

Any object that is accessed has an effective type, which if it isn't
anything else is the type of the lvalue used to access it; section
6.5 p6.

Some objects have an "inherent type" in the sense that the memory
corresponds to (an instance of) a declared variable, and the variable
has a type. This "inherent type" shows up tacitly in the language of
6.5 p6, eg, "the declared type of the object, if any" (despite
effective type being defined only when accessing objects, and despite
the small inconsistency that variables, not objects, have a type
declared for them). Section 6.5 p6 specifically anticipates the
circumstance that an object not have an "inherent type", eg, "If a
value is stored into an object having no declared type ...".

Section 6.5 p7 imposes requirements on the relationship between the
type of an object (that was designated) and the effective type of an
object (that was accessed). Apparently there are no requirements
imposed on the relationship between type and effective type (or
"inherent type" for that matter), except in the case of access.

The language in the standard is confused about what the word "object"
means. It's defined (3.14) as 'a region of data storage in the
execution environment', but in lots of places in the text of the
standard it's used to mean something more like "an instance of a
variable". There is the example language above, talking about the
'declared type of the object', despite there being no definition of
what the "declared type" of an object is (at least not that I could
find). Also, although I don't have a reference, I'm pretty sure
I remember seeing a phrase like "an instance of the object", which
I took to mean that it's being used basically as a synonym for
variable.

To respond to the comment above - whenever an object is designated it
has a type, which is the type of the expression used to designate the
object; it is this type that is used to interpret what value might
be represented.

A region of storage need not have any type. Whenver that region of
storage takes part in a computation, a type is imposed on it by the
expression used to access it.
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
11
by: Lues | last post by:
Hi, I'm trying to protect some data in tables with encription (you know why, don't you ;)) I must confess that I'm not very expirienced in writing code, especially encription code. Can any...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
2
by: Tiraman :-\) | last post by:
Hi Everyone, i have the following problem in my client-server Application My server take array and serialize it into the memorystream Dim ns As NetworkStream = client.GetStream() Dim writer...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
3
by: JJA | last post by:
I am confused about all the various methods of handling XML in ASP.Net 2.0. I want to be able to stream XML to some clientside Javascript code (snippet below): var urlRequest =...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.