简体   繁体   English

如何将字符串转换为字节数组?

[英]How to convert a string to byte array?

I have the following char*: 我有以下字符*:

char*Problem = "\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04"

I am trying to get this: 我试图得到这个:

unsigned char buf[] = {0x8B, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x7C, 0x24, 0x14, 0x85, 0xC9, 0x74, 0x16, 0x8B, 0x03, 0x89, 0x01, 0x8B, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x15, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x83, 0xC1, 0x04};

But by runtime. 但是通过运行时。 I am reading the char* from a textbox. 我正在从文本框中读取char *。

I tried the following: 我尝试了以下方法:

UnicodeString Maskstr = Edit2->Text; //My char*Problem above
const char *chr = AnsiString(Maskstr).c_str();
char* MaskConverted = const_cast<char*>( chr );
unsigned char NewArray[40];
strcpy( (char*) NewArray, MaskConverted  );

I thought, my unsigned char would be NewArray now, but it´s not. 我以为,我的未签名字符现在应该是NewArray,但事实并非如此。 So how to get the correct result?.. 那么如何获得正确的结果呢?

I am a bit stuck right now. 我现在有点卡住了。 Any ideas? 有任何想法吗?

Thank you! 谢谢! :) :)

Your example improperly initializes the Problem variable (it is truncating data) after the 2nd byte. 您的示例在第二个字节之后不正确地初始化了Problem变量(正在截断数据)。

But let's just assume for the sake of argument that your TEdit actually contains the complete text you are interested in. Does the TEdit contain actual raw bytes that have been extended as-is to 16-bit Unicode chars, or does it contain a hex-encoded representation of the raw bytes? 但是,为了便于讨论,我们假设您的TEdit实际上包含您感兴趣的完整文本TEdit是否包含已按原样扩展为16位Unicode字符的实际原始字节 ,还是包含十六进制的-原始字节的编码表示形式 This makes a BIG difference in how you process the text. 这使您在处理文本方面大不同。

If the text contains raw bytes , then your processing code is incorrectly storing a char* pointer to data that belongs to a temporary AnsiString that gets destroyed before you use the pointer, thus exhibiting undefined behavior . 如果文本包含原始字节 ,则您的处理代码错误地存储了char*指针,该指针指向的数据属于临时AnsiString ,在使用该指针之前会被破坏,从而表现出未定义的行为 Don't destroy the AnsiString until after you have copied from it: 复制完AnsiString之前,请勿破坏它:

AnsiString Maskstr = Edit2->Text;
unsigned char NewArray[40];
strncpy(reinterpret_cast<char*>(NewArray), const_cast<char*>(s.c_str()), 40);

Alternatively, simply don't convert to AnsiString at all, you can just truncate the Unicode characters instead: 另外,根本不转换为AnsiString ,您可以截断Unicode字符:

UnicodeString Maskstr = Edit2->Text;
unsigned char NewArray[40] = {};
int maxlen = Maskstr.Length();
if (maxlen > 40) maxlen = 40;
for(int i = 0; i < maxlen; ++i) {
    NewArray = static_cast<unsigned char>(Maskstr[i+1]);
}

However, if the text actually contains a hex-encoded representation of raw bytes, then you need to parse the text to decode the hex sequences into actual bytes, eg: 但是,如果文本实际包含原始字节的十六进制编码表示形式 ,则需要解析文本以将十六进制序列解码为实际字节,例如:

#include <stdio.h>

UnicodeString Maskstr = Edit2->Text;
const WideChar *ptr = Maskstr.c_str();
unsigned char NewArray[40] = {};
int maxlen = Maskstr.Length();
if (maxlen > 40) maxlen = 40;
for(int i = 0; i < maxlen; ++i)
{
    unsigned int b, numChars;
    if (swscanf(ptr, L"\\x%2x%n", &b, &numChars) != 1) break;
    NewArray[i] = static_cast<unsigned char>(b);
    ptr += numChars;
}

The compiler will compile tabbed hex values but I couldn't find a function to translate it - although Remy is using a scanf. 编译器将编译制表符的十六进制值,但是我找不到转换它的函数-尽管Remy使用的是scanf。 I changed the tab to 0x format and used strtol. 我将选项卡更改为0x格式并使用strtol。

// convert hex string to char/byte values
size_t TForm1::TranslateTabbedHex(const AnsiString str, std::vector<unsigned char> & result)
{
  AnsiString char_str(str);
  char * pEnd;

  size_t pos = char_str.AnsiPos("\\");
  for( ; pos; pos = char_str.AnsiPos("\\") )
  {
    if( pos != 1)
      char_str.Delete(1, pos-1);

    char_str[1] = '0';
    result.push_back( strtol(char_str.c_str(), &pEnd, 0) );
    char_str.Delete(1,1);
  }

  return result.size();
}
//---------------------------------------------------------------------------

void TForm1::TestTranslateTabbedHex(std::vector<unsigned char> & result)
{
  unsigned char test_value[] = "\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14"
                               "\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D"
                               "\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00"
                               "\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04";

  //"\x8B\x15\x00\x00\x00\x00\x8B\x7C\x24\x14\x85\xC9\x74\x16\x8B\x03\x89\x01\x8B\x2D\x00\x00\x00\x00\x8B\x15\x00\x00\x00\x00\x8B\x0D\x00\x00\x00\x00\x83\xC1\x04"

  Memo1->Clear();

  size_t test_size =  sizeof test_value - 1;
  AnsiString length( test_size );
  Memo1->Lines->Add( AnsiString("size test_value=") + length );
  Memo1->Lines->Add( AnsiString("size result=") + result.size() );

  AnsiString text(Edit1->Text);
  if(Edit1->Text.Length() > 0)
    Memo1->Lines->Add( AnsiString("first char value in EditBox = ") + text[1]);
  else
    Memo1->Lines->Add("No text in EditBox");

  Memo1->Lines->Add(" ");

  if(result.size() == 0)
  { Memo1->Lines->Add("result = 0 length");
  }
  else
  { size_t test_size = std::min( result.size(), test_size );  // #include <algorithm>
    Memo1->Lines->Add("test  - result - same");
    for(size_t i=0; i<test_size; i++)
    { AnsiString line(test_value[i]);
      line += AnsiString("  -  ") + result[i];
      line += AnsiString("  -  ") + AnsiString((result[i] == test_value[i]) ? "Yes" : "No");
      Memo1->Lines->Add( line );
    }
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::vector<unsigned char> result;
  TranslateTabbedHex(Edit1->Text, result);
  TestTranslateTabbedHex( result );
}
//---------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM