473,388 Members | 1,405 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,388 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 3132

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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
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...

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.