Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to assign a string to a Uint8 variable, it didn't work to typecast or anything like that so what I tried to do first was to assign via a file so that I would first write the string to stderr.txt and then use fscanf to read the string. I used fscanf because you can choose what type to read with and so then I would have successfully converted the string to Uint8, right? Wrong! I then found out that the program doesn't write normal characters during execution so that if the string contained for example "Test", then the Uint8 would contain something like "(!µ@" because the file didn't contain normal characters during execution.
Now my question is simply this: how do I assign a string to a Uint8 variable so that if the string contains "Test", then the Uint8 variable will conatin "Test"?(but in a diffrent format of course)
So i would like to use it like this:
Uint8 data;
string str;
data = str;

*do whatever*
Posted
Updated 24-Sep-11 23:42pm
v2
Comments
Philippe Mori 24-Sep-11 16:50pm    
Well, your question is not clear... and does not make any sense.

First, it is tagged as C++/CLI but it seems you are using C++ types and not .NET types (System::String and System::Byte).

Then Uint8 as spelled come from where?

Does your string type is std::string from the C++ librairies or the .NET type for C# string (that is System::String)?

Also by assing, do you mean some kind of cast of a conversion to a numeric type or the value of the first characters.

Finaly, your code sample it not clear. Does the string is a textual number? Something like string str = "23" and you would want data to have a value of 23?

Also since normally Uint8 is a typedef for a single character, then your string "(!µ@" is not really possible.

Also if your string str contains "Test" what do you expect Uint8 to contains.

And how can you have "(!µ@" when the code is not even supposed to compile!
Karsten Malmquist 24-Sep-11 17:42pm    
My question was poorly written; I am using C++, not .NET. My question was how to get the contents of a std::string and put it into a UDP packet (I am making a server application), the UDP packet has a member called "data", but it was kind of hard to explain how the Uint8 variable was declared since I can't find the code of the structure that the variable is a member of. I would imagine that the variable is an array since you can fit multiple characters in it, but the fact that there is no indexing used also kind of eliminates that.
Also, with "(!µ@" I meant that when you write to a file using the fprintf function, then it writes some stuff that is not normal characters to the file during execution, and when you used the fscanf function to read the text from the file, then it read those weird characters so that it could for example read "(!µ@".
Philippe Mori 24-Sep-11 21:46pm    
Well, find the declaration of the UDP structure and also show us the actual code fprintf and fscanf. By the way, if you want to ouptup a std::string with fprintf, you have to call member function c_str() since fprintf is declared with ... as it allows variable arguments.
Sergey Alexandrovich Kryukov 25-Sep-11 2:48am    
If you use C++/CLI, you certainly use .NET (or Mono, or other CLR framework).
You have no idea what are you asking about. Not knowing either the language you use or the platform is the shame, and you certainly don't know at least one of them.
--SA

From MSDN[^]:
UINT8
An 8-bit unsigned integer (range: 0 through 255 decimal). Because a UINT8 is unsigned, its first bit (Most Significant Bit (MSB)) is not reserved for signing.

This type is declared in BaseTsd.h as follows:
C++
typedef unsigned char UINT8;

So you cannot assign a string to an UINT8 variable. You can however assign a char to an UINT8 variable.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-11 2:46am    
My 5, but the answer is too long. One word would suffice: "gibberish". :-)
--SA
Albert Holguin 25-Sep-11 17:15pm    
See Richard's answer, think that's what the OP meant...
C++
Uint8* pdata;
string str;
pData = new Uint8[str.length() + 1];
memcpy (pdata, str.c_str(), str.length() + 1);

Remember you cannot cast from one type to another in the way you tried; a string and a Uint8 are not in any way compatible.
 
Share this answer
 
v2
Comments
Albert Holguin 25-Sep-11 17:14pm    
This does exactly what he needs (after reading the UDP info he posted above). +5
Richard MacCutchan 26-Sep-11 4:51am    
Thanks; it still amazes me that someone can be writing a UDP server application and yet still does not understand that casting a string to a Uint8 will not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900