473,657 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14858
cp**********@ya hoo.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.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #2
cp**********@ya hoo.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**********@ya hoo.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**********@ya hoo.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**********@ya hoo.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*****@mindsp ring.com> scribbled the following:
Ari Lukumies wrote:
Martin Ambuhl wrote:
> cp**********@ya hoo.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.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 14 '05 #7
Joona I Palaste wrote:
pete <pf*****@mindsp ring.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
implementat ion.

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********@com cast.net> wrote:
Joona I Palaste wrote:
pete <pf*****@mindsp ring.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********@com cast.net> wrote:

Joona I Palaste wrote:
pete <pf*****@mindsp ring.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

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

Similar topics

5
3345
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 printer using a RawPrinterHelper class that i found I believe in the msdn. the class works wonderfully when I send stright text data (in the correctly formated string that the sato printer requires.) but when i go to send a image nothing is...
11
3530
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 one, please , send VB code for access which I can c/p into one function. It don't have to be RSA, it can be anything which is easy to
7
3301
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
6
10189
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
1
2297
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
1501
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 As New IO.StreamWriter(ns) Dim bf As New BinaryFormatter Dim mem As New IO.MemoryStream
16
5440
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
3
3379
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 = "../GetLendersAsXML.aspx" var request = GXmlHttp.create(); request.open("GET", urlRequest, true); request.onreadystatechange = function() { if (request.readyState == 4) { var xmlDoc = request.responseXML;
0
10751
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7333
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.