473,699 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to translate (unsigned char []) to string?

unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

Apr 23 '07 #1
7 21935
"Zhang Liming" <xp********@gma il.comwrote in message
news:f0******** **@news.yaako.c om...
unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?
string str( rcd );
is probably the best way if you can do it.

Otherwise I guess
str = rcd;

is what you need.
Apr 23 '07 #2
Jim Langston wrote:
"Zhang Liming" <xp********@gma il.comwrote in message
news:f0******** **@news.yaako.c om...
>>unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

string str( rcd );
is probably the best way if you can do it.

Otherwise I guess
str = rcd;

is what you need.
Not if rcd is array of *unsigned* char.

If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.

--
Ian Collins.
Apr 23 '07 #3
Yes, you should confirm there is the terminating '\0' at rcd[9] or
before.
Then you can pass (char *)rcd to the string constructor.
On 4ÔÂ23ÈÕ, ÉÏÎç11ʱ44·Ö, Ian Collins <ian-n...@hotmail.co mwrote:
Jim Langston wrote:
"Zhang Liming" <xphenix...@gma il.comwrote in message
news:f0******** **@news.yaako.c om...
>unsigned char rcd[10];
>rcd contains 10 chars, such as '1', '2'.... etc.
>string str;
>the problem is how to pass the contents in rcd to str with minimal cost?
string str( rcd );
is probably the best way if you can do it.
Otherwise I guess
str = rcd;
is what you need.

Not if rcd is array of *unsigned* char.

If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.

--
Ian Collins.

Apr 23 '07 #4
On Apr 22, 10:07 pm, Zhang Liming <xphenix...@gma il.comwrote:
unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

xphenix.cn.vcf
1KDownload
wouldn't string concatenation would like

str += rcd;

this would just cause it to be appended to the end of str and then you
can just concatenate another '\0'

Apr 23 '07 #5
ad*********@gma il.com wrote:
On Apr 22, 10:07 pm, Zhang Liming <xphenix...@gma il.comwrote:
>>unsigned char rcd[10];

rcd contains 10 chars, such as '1', '2'.... etc.

string str;

the problem is how to pass the contents in rcd to str with minimal cost?

xphenix.cn.v cf
1KDownload


wouldn't string concatenation would like

str += rcd;

this would just cause it to be appended to the end of str and then you
can just concatenate another '\0'
No it wouldn't in this case because rcd is an array of *unsigned* char.

--
Ian Collins.
Apr 23 '07 #6
On Apr 23, 5:07 am, Zhang Liming <xphenix...@gma il.comwrote:
unsigned char rcd[10];
rcd contains 10 chars, such as '1', '2'.... etc.
So why is it declared "unsigned char"?
string str;
the problem is how to pass the contents in rcd to str with
minimal cost?
If you know the length, and what to avoid unsightly casts:-),
you can use the two argument template constructor for
std::string:

std::string s( rcd, rcd + N ) ;

Otherwise, reinterpret_cas t can be used:

std::string s( reinterpret_cas t< char const* >( rdc ), N ) ;

, or, if the "string" in rcd is '\0' terminated:

std::string s( reinterpret_cas t< char const* >( rdc ) ) ;

But I think the source of your problem is the original
declaration. The usual convensions are:

characters: char
small integers: signed char
raw memory ("bytes" or bits): unsigned char

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 23 '07 #7

"Ian Collins" <ia******@hotma il.comwrote in message
news:59******** ******@mid.indi vidual.net...
If the array is known to be zero terminated, you might be able to get
away with a cast, otherwise you'd have to do something like memcpy to an
array of char and add the terminating '\0'. It all depends what is in
the original array.

--
Ian Collins.
The array doesn't need to be zero-terminated:

std::string s(buffer, buffer + 10);
// or
s.assign(buffer , buffer + 10);

- Sylvester Hesp
Apr 23 '07 #8

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

Similar topics

33
15545
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In fact, they may not even represent complete characters. At this point I think of text as simple binary blobs. What charset, character encoding and termination they use should not be exposed in the interface used to operate on them. But now I have...
0
8685
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
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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
9032
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
8908
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
7745
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
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.