Solved

converting a std::string to BYTE[16]

Posted on 2004-04-10
10
563 Views
Last Modified: 2008-02-01
hi,
i need some help in convert a std::string to BYTE[16]. does anyone have any idea on how to do so? thanks!

-ashrobo
0
Comment
Question by:ashrobo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 8

Assisted Solution

by:mnashadka
mnashadka earned 25 total points
ID: 10797567
If you need to create a new variable, you can cast the BYTE array to a char * and use strncpy to get the value:
  BYTE bytes[16] = {0};
  std::string str = "hello";
  // Copy up to 16 characters into bytes
  strncpy((char *)bytes, str.c_str(), 16); // You could also use reinterpret_cast here

If you need to just use an existing variable in a function call, you can cast the string.c_str() to a BYTE *, but this could be an issue if the size is wrong:
myfunc((BYTE *)str.c_str()); // You could also use reinterpret_cast here

Hope this helps.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10797586
>>myfunc((BYTE *)str.c_str());
FYI:
This method would be unsafe, since you're removing the constantness from the pointer.
Recommend putting the constant into the cast:
myfunc((const BYTE *)str.c_str());
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 25 total points
ID: 10797591
BYTE b[16];
string s = "12345678";
unsigned int unLen = s.size();
memset ( b, 0, sizeof(b));
memcpy ( b, s.c_str(), unLen > sizeof(b) ? sizeof(b) : unLen);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Accepted Solution

by:
Axter earned 75 total points
ID: 10797612
jkr,
I think you're method is safer then the code used for the previous strncpy method.
But just to add a little more efficientcy you could remove the memset, and add a nullifier after copying the string:
Example:

memcpy ( b, s.c_str(), unLen > sizeof(b) ? sizeof(b) : unLen);
b[unLen] = 0;
0
 
LVL 30

Expert Comment

by:Axter
ID: 10797619
Correction:

memcpy ( b, s.c_str(), unLen > sizeof(b) ? sizeof(b) : unLen);
if (unLen < sizeof(b)) b[unLen] = 0;
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10797834
>>Recommend putting the constant into the cast:
>>myfunc((const BYTE *)str.c_str());

Axter, I agree typically, but that depends upon whether the function takes a const or not.  I was assuming that the function took a BYTE * as a parameter, at which point putting the const there will just make you have to do a const_cast or whatever.  Even if it should be const in the function prototype, there are a lot of programmers out there who, for one reason or another (usually training), don't make the parameter const in the function.
0
 

Author Comment

by:ashrobo
ID: 11003144
i'm sorry for not replying, i haven't had the time to try the solutions provided. how can i deal with this?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 115
Unresolved External Symbols 3 158
c++, dynamic object by json 1 65
cmake and message 1 23
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question