Solved

converting a std::string to BYTE[16]

Posted on 2004-04-10
10
549 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
10 Comments
 
LVL 8

Assisted Solution

by:mnashadka
mnashadka earned 25 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Accepted Solution

by:
Axter earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now