I currently use the conversion function which looks like this (C++ Builder):
UnicodeString OutputInCodePage(const int CodePage, std::string& InStr, const unsigned Offs)
{
switch (CodePage)
{
default:
case 0: return AnsiStringT<0> (InStr.substr(Offs).c_str()); // System default
case 037: return AnsiStringT<037> (InStr.substr(Offs).c_str()); // IBM037 IBM EBCDIC US-Canada
case 437: return AnsiStringT<437> (InStr.substr(Offs).c_str()); // IBM437 OEM United States
case 500: return AnsiStringT<500> (InStr.substr(Offs).c_str()); // IBM500 IBM EBCDIC International
// And many more following...
}
}
But I really want to avoid the use of huge switch/case - something like this:
UnicodeString OutputInCodePage(const int CodePage, std::string& InStr, const unsigned Offs)
{
return AnsiStringT<CodePage> (InStr.substr(Offs).c_str());
}
That doesn't work and gives me E2396 Template argument must be a constant expression error.
Is there a way to re-do the code so I can use std::string raw data conversion (into UnicodeString) based on CodePage parameter without a huge switch/case list?
There is indeed a much easier way to handle this - use the RawByteString type and the SetCodePage() function, eg:
UnicodeString OutputInCodePage(const int CodePage, const std::string &InStr, const unsigned Offs)
{
RawByteString out(InStr.c_str() + Offs);
SetCodePage(out, CodePage, false);
return out;
}
Alternatively, use the UnicodeFromLocaleChars() function:
UnicodeString OutputInCodePage(const int CodePage, const std::string &InStr, const unsigned Offs)
{
UnicodeString out;
const char *p_in = InStr.c_str() + Offs;
int in_len = InStr.length() - Offs;
int out_len = UnicodeFromLocaleChars(CodePage, 0, p_in, in_len, NULL, 0);
if (out_len > 0)
{
out.SetLength(out_len);
UnicodeFromLocaleChars(CodePage, 0, p_in, in_len, out.c_str(), out_len);
}
return out;
}
Related
void gen_rand(unsigned char rand[], int len)
{
time_t time = time();
int i;
for(i=0;i<len;i++)
rand[i] = time+i;
}
void encrypt(unsigned char key[], int ken_len, unsigned char text[], int text_len, unsigned char out[], int &len_out)
{
......
}
void hexdump(unsigned char *buf, int len)
{
......
}
void enc_test(unsigned char text[], int len_text, unsigned char out[], int &out_len)
{
unsigned char key[32];
gen_rand(key,32);
encrypt(key,32,text,32,enc,enc, out_len);
}
int main()
{
int i,out_len,j;
unsigned char text[32];
unsigned char enc[64];
for(i=0;i<65536;i++)
{
for(j=0;j<32;j++) text[j] = i;
enc_test(text,32,enc,out_len);
hexdump(enc,out_len);
sleep(1);
}
}
Above is a sample program in linux. My need is to read the local variable key in function enc_test every time this function runs. Maybe gdb or ptrace is a good option, but this method might significantly slow down the program's speed. Is there a method to monitor and read a local variable when its value changes? How to determine the address of a local variable if ASLR is enabled?
I am new in programming. Actually I am in 2nd year of college and starting an internship. They want me to do a program in C++ builder but I only know C. What I studied. And I don't have any knowledge about OOP.
So my question is.
I have TEdit1 and I want to verify if the data introduced in that textbox is a number. I know to verify if it is a number but I don't know how to put data from TEdit into string.
I wrote some code but it doesn't work.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Size = Edit1->GetTextLen(); //Get length of string in Edit1
Size++; //Add room for null character
Char *Buffer = new Char[Size]; //Creates Buffer dynamic variable
std::auto_ptr<Char> Buffer(new Char[Size]);
Edit1->GetTextBuf(Buffer.get(),Size); //Puts Edit1->Text into Buffer
ShowMessage(Buffer);
}
And I get these errors:
E2034 Cannot convert 'std::auto_ptr<wchar_t>' to 'UnicodeString'
E2342 Type mismatch in parameter 'Msg' (wanted 'const UnicodeString', got std::auto_ptr<wchar_t>')
Can you please explain what i did wrong, or where i can found Embarcadero C++ Builder tutorials? I searched all google and didn't find something to help me.
Your code has several mistakes in it:
you are declaring two Buffer variables in the same scope. That is not allowed. You need to remove one of them.
you are passing the std::auto_ptr itself to ShowMessage(), but it expects a System::UnicodeString instead, thus the compiler error message. You can use the std::auto_ptr::get() method to get the wchar_t* pointer and pass it to ShowMessage(), as UnicodeString has a constructor that accepts wchar_t* as input:
ShowMessage(Buffer.get());
despite the above, you actually cannot use a pointer from new[] with std::auto_ptr to begin with. std::auto_ptr uses delete instead of delete[] to free the memory being pointed at. You must always use delete with new, and delete[] with new[]. So, while the code will compile, it will not free the memory correctly at runtime. C++11 introduced a new std::unique_ptr class to replace std::auto_ptr, and std::unique_ptr supports new[] and delete[] (however, C++Builder's 32bit compiler does not support C++11 yet - that is in the works - but its 64bit compiler does):
#include <memory>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int Size = Edit1->GetTextLen(); //Get length of string in Edit1
Size++; //Add room for null character
std::unique_ptr<Char[]> Buffer(new Char[Size]); //Creates Buffer dynamic variable
Edit1->GetTextBuf(Buffer.get(), Size); //Puts Edit1->Text into Buffer
ShowMessage(Buffer.get());
}
Now, with that said, if you are going to continue using C++Builder, you should learn how to use its built-in functionalities, like UnicodeString, which the RTL/VCL relies heavily on (use the System::String alias for most code, use UnicodeString directly only when absolutely necessary).
Your example can be vastly simplified using the TEdit::Text property:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String s = Edit1->Text; //Get string in Edit1
ShowMessage(s);
}
The simplest solution to your problem would be to use the TCSpinEdit component instead of TEdit, as TCSpinEdit only allows numeric input in the first place, and has a Value property that returns an int:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number = CSpinEdit1->Value;
ShowMessage("It is a number");
// use number as needed...
}
But, if you have to stick with TEdit, there are many ways to check a UnicodeString for numeric content.
You can set the TEdit::NumbersOnly property to true so the user cannot enter a non-numeric value (unless they use copy/paste, but let's ignore that for the moment), and then use the RTL's StrToInt() function, or System::UnicodeString::ToInt() method, to convert it as-is:
#include <System.SysUtils.hpp>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number;
try
{
number = StrToInt(Edit1->Text);
// or: number = Edit1->Text.ToInt();
}
catch (const EConvertError&)
{
// not a number, do something else...
ShowMessage("It is not a number");
return;
}
ShowMessage("It is a number");
// use number as needed...
}
Or you can use the RTL's System::Sysutils::TryStrToInt() function:
#include <System.SysUtils.hpp>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number;
if (TryStrToInt(Edit1->Text, number))
{
// use number as needed...
ShowMessage("It is a number");
}
else
{
// not a number, do something else...
ShowMessage("It is not a number");
}
}
Or you can use the STL's std::wistringstream class:
#include <sstream>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number;
std::wistringstream iss(Edit1->Text.c_str());
if (iss >> number)
{
// use number as needed...
ShowMessage("It is a number");
}
else
{
// not a number, do something else...
ShowMessage("It is not a number");
}
}
Or, since you have a C background, you can use the C _wtoi() function (which doesn't offer much in the way of error checking):
#include <cstdlib>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number = std::_wtoi(Edit1->Text.c_str());
if (number != 0)
{
// use number as needed...
ShowMessage("It is a valid number");
}
else
{
// not a number, do something else...
ShowMessage("It is not a valid number");
}
}
Or you can use the C wcstol() function:
#include <cstdlib>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String s = Edit1->Text;
Char *p = s.c_str(), *end;
int number = std::wcstol(p, &end, 10);
if (end != p)
{
// use number as needed...
ShowMessage("It is a number");
}
else
{
// not a number, do something else...
ShowMessage("It is not a number");
}
}
Or you can use the C swscanf() function:
#include <cstdio>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int number;
if (std::swscanf(Edit1->Text.c_str(), L"%d", &number) == 1)
{
// use number as needed...
ShowMessage("It is a number");
}
else
{
// not a number, do something else...
ShowMessage("It is not a number");
}
}
My environment: Xcode5, iOS, Objective-C/Objective-C++ mix.
I am trying to figure out what causes the next problem. I am writing my own logging function:
int _me_log(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char *c = va_arg(args, char *);
char *message = NULL;
printf(fmt, args);
int n = asprintf(&message, fmt, args);
if (n != -1 && message != NULL) {
//do something with 'message' like writing to file, etc.
UPDATE:
//we need to handle memory created for 'message' storage.
free(message);
}
va_end(args);
return n;
}
Then I call it like this:
_me_log("socket %s did open", "Socket: 0x1fd1c880");
And instead of correct output socket Socket: 0x1fd1c880 did open I get some gibberish like this socket \\323\331/ did open in this line printf(fmt, args);.
If I call it this way printf("%s", c); I get correct results.
I have googled several implementations (this or this ) of logging functions and functions which pass variable parameters and it seems that I do everything correctly.
Could you please suggest me what I'm doing wrong?
You've got the right idea to use va_list here, but if you work with va_list you should use vasprintf instead of asprintf:
int _me_log(const char *fmt, ...)
{
va_list args;
char *message = NULL;
int n;
va_start(args, fmt);
n = vasprintf(&message, fmt, args);
if (n != -1 && message != NULL) {
// ... use message ...
}
free(message);
va_end(args);
return n;
}
For every routine of the printf family, there is a variant that takes a va_list instead of the variadic argument ... and whose name is prefixed with the letter v, for example:
int printf(const char *format, ...);
int vprintf(const char *format, va_list ap);
These routines exist so you can write you own (non-macro) wrapper for xprintf.
Seems like a very complicated implementation. Try:
int _me_log(const char *fmt, ...) {
int ret = 0;
va_list va;
va_start(va, fmt);
ret = vprintf(fmt, va);
va_end(va);
putc('\n', stdout);
return ret;
}
But, of course, that is no different from printf(), except for forcing a newline.
I have the following C code that uses libmodbus to read a single device register using ModbusTCP:
modbus_t *ctx;
uint16_t tab_reg[16];
ctx = modbus_new_tcp("10.0.1.77", 502);
modbus_read_registers(ctx, 0x20, 2, tab_reg);
printf("reg = %d (0x%X)\n", tab_reg[0], tab_reg[0]);
printf("reg = %d (0x%X)\n", tab_reg[1], tab_reg[1]);
now trying to switch this over to Vala using a Vapi that I've generated, the contents of that for new and read are:
[CCode (cheader_filename = "modbus.h", cname = "modbus_new_tcp")]
public static unowned Modbus.modbus_t create_tcp (string ip_address, int port);
public static int read_registers (Modbus.modbus_t ctx, int addr, int nb, uint16 dest);
[CCode (cheader_filename = "modbus.h")]
and the translated Vala program is:
class ModbusReadTest : GLib.Object {
unowned Modbus.modbus_t ctx;
public void run () {
uint16 reg = 0x00;
ctx = create_tcp ("10.0.1.77", 502);
Modbus.read_registers (ctx, 0x20, 2, reg);
message ("reg = %d (0x%X)", reg, reg);
Modbus.close(ctx);
}
}
Coincidentally, when I compile this into C code and then into a binary using gcc I get the error:
read-registers-test.c:71:2: warning: passing argument 4 of ‘modbus_read_registers’ makes pointer from integer without a cast [enabled by default]
which is not surprising. But I'm not sure how I should go about modifying the Vapi contents to closer match the prototype in the libmodbus header:
int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
I've tried a mix of array options and using 'out', but haven't been able to get more than a single double byte register at a time.
read_registers should probably be an instance method (on Modbus.modbus_t) instead of a static method, and Modbus.modbus_t should probably be renamed to something like Modbus.Context, create_tcp should probably be a constructor, and Modbus.close should be a free function on the Modbus.Context compact class, but that's beside the point of this question (if you stop by #vala on irc.gnome.org you can get help with that stuff).
You probably want to make it an array:
public static int read_registers (Modbus.modbus_t ctx, int addr, [CCode (array_length_pos = 2.5)] uint16[] dest);
Then you would do something like this in Vala:
public void run () {
uint16 reg[2];
ctx = create_tcp ("10.0.1.77", 502);
Modbus.read_registers (ctx, 0x20, reg);
message ("reg = %d (0x%X)", reg, reg);
Modbus.close(ctx);
}
For a port more faithful to the original C (where tab_reg has 16 elements instead of 2), you could use array slicing:
public void run () {
uint16 reg[16];
ctx = create_tcp ("10.0.1.77", 502);
Modbus.read_registers (ctx, 0x20, reg[0:2]);
stdout.printf ("reg = %d (0x%X)\n", reg, reg);
Modbus.close(ctx);
}
Note that if you make it an instance method you'll need to change the array_length_pos to 1.5.
I am trying to call a function in C from C# though c ++
so basically C# -> C++ - >C
In C#, I have byte[] bytes - which reads the information from the file. I am passing the byte array and the size to C++ .
In C++ I get the byte array and the size but I am not able to convert to the specific data types.
void Image::OpenMemFile(array<Byte>^ data, unsigned int size)
{
Free();
m_dataStream = data;
Byte const* streamData = &data[0]; // this is where it throws error
// Should I use marshaling here ? What call should that ;be ?
hImage = ::OpenMemImage(streamData , size);
modified = false;
}
// this is the function I need to call
EXIVSIMPLE_API HIMAGE OpenMemImage(const BYTE *data, unsigned int size)
{
// code
imgWrap->image = Exiv2::ImageFactory::open(data, size);
}
the C function it needs to call is
Image::AutoPtr ImageFactory::open(const byte* data, long size)
{
/// code
}
I need to help in converting the byte array to const byte* . I realize I need to use Marshaling. Is there a specific function to marshal arrays in C++ ?
Any help is appreciated.
Thanks
pin_ptr<unsigned char> pin_buffer = &data[0];
unsigned char* pData = pin_buffer;