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

std::string to char[] with a twist

Hello,

I need your expertise to perform a conversion.

I need to convert a std::string to a char[32]. However, in the
conversion, I need to get rid of the '/0' character. The initial string
represents a 256 bit (32 byte) numeric value. Therefore, the resulting
char[32] should not contain '/0'.

If you have any suggestions, please let me know.

Aug 8 '06 #1
3 3133

ruffiano napísal(a):
Hello,

I need your expertise to perform a conversion.

I need to convert a std::string to a char[32]. However, in the
conversion, I need to get rid of the '/0' character. The initial string
represents a 256 bit (32 byte) numeric value. Therefore, the resulting
char[32] should not contain '/0'.

If you have any suggestions, please let me know.
How about:

std::string src;
char dst[32];
memcpy(dst, src.data(), 32);

Plus some additional bounds checking, etc.

Aug 8 '06 #2
ruffiano posted:
Hello,

I need your expertise to perform a conversion.

I need to convert a std::string to a char[32]. However, in the
conversion, I need to get rid of the '/0' character. The initial string
represents a 256 bit (32 byte) numeric value. Therefore, the resulting
char[32] should not contain '/0'.

There's no need to get rid of the null terminator; just pretend it isn't
there.

If you have any suggestions, please let me know.

#include <cassert>
#include <string>
#include <iostream>

using std::string;
using std::cout;

unsigned const magic = 32;

char const inline (&Func(string const &str))[magic]
{
char const *const p = str.c_str();

assert(strlen(p) >= magic);

return reinterpret_cast<char const (&)[magic]>(*p);
}

int main()
{
string str("abcdefghijklmnopqrstuvwxyzabcdef");

char const (&arr)[magic] = Func(str);

char const *p = arr;
char const *const p_over = arr + magic;

do
{
cout << *p++;
} while (p != p_over);
}

--

Frederick Gotham
Aug 8 '06 #3

ruffiano wrote:
Hello,

I need your expertise to perform a conversion.

I need to convert a std::string to a char[32]. However, in the
conversion, I need to get rid of the '/0' character. The initial string
represents a 256 bit (32 byte) numeric value. Therefore, the resulting
char[32] should not contain '/0'.

If you have any suggestions, please let me know.
Why do you think you need to do this? You might not.

Aug 8 '06 #4

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
1
by: ruffiano | last post by:
Hello, I need your expertise to perform a conversion. I need to convert a std::string to a char. However, in the conversion, I need to get rid of the '/0' character. The initial string...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
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
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
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...
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.