| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Convert String to unsigned char
Hi Everyone,
This should be pretty simple but I'm just not getting it. I am trying to bring a string variable into a method, then assign that String variable into a unsigned char but I'm not sure how to convert it. Here is the code: unsigned char szStr[25] = dbServer; The value of dbServer is 12.12.12.12 and when I try: unsigned char szStr[25] = "12.12.12.12"; it works. Any thoughts? Thanks in advance! |
|
#2
|
|||
|
|||
|
Re: Convert String to unsigned char
Actually, I'm thinking the best way to do this is to:
1.) Find the string length 2.) Loop through the string 3.) Assign each char ripped out of the string to szStr[0], szStr[1]...szStr[n] This would also enable me to set the correct length of the vector, no more, no less. I will post back with my code once this works. Thanks! |
|
#3
|
|||
|
|||
|
Re: Convert String to unsigned char
Assuming the string you pass in uses char as its underlying character type, you can just reinterpret_cast the char* to an unsigned char*. Normally reinterpret_cast is frowned upon, but it wouldn't make a difference here.
If there's any possibility that the string might be using wchar_ts as the underlying type, you can't do that. |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|