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

时间:2019-07-04 22:01:04

标签: c++ vcl

我有以下字符*:

<table border="1">
  <tr>
    <th>Clinical Trials ID</th>
    <th>Official Title</th>
    <th>Contact Name</th>
    <th>Affiliation</th>
    <th>Phone</th>
    <th>email</th>
  </tr>
  <tr>
    <td><a href="https://clinicaltrials.gov/ct2/show/NCT01951326">NCT01951326</a></td>
    <td>A Randomized, Double Blind, Placebo-controlled, Multicenter, Parallel Group Study to Assess the Efficacy and Safety of Fixed-dose Combination RHB-104 in Subjects With Moderately to Severely Active Crohn's Disease</td>
    <td> Ira N Kalfus, MD</td>
    <td>RedHill Biopharma</td>
    <td> </td>
    <td></td>
  </tr>
</table>

我正试图得到这个:

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"

但是在运行时。 我正在从文本框中读取char *。

我尝试了以下操作:

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};

我想,我的未签名字符现在应该是NewArray,但事实并非如此。那么如何获得正确的结果呢?

我现在有点卡住了。 有什么想法吗?

谢谢! :)

2 个答案:

答案 0 :(得分:1)

您的示例在第二个字节之后错误地初始化了Problem变量(正在截断数据)。

但是,为了便于讨论,我们假设您的TEdit实际上包含了您感兴趣的完整文本。TEdit是否包含实际的原始字节?扩展为16位Unicode字符,还是包含原始字节的十六进制编码表示形式?这使您在处理文本方面大不同。

如果文本包含原始字节,则说明您的处理代码错误地存储了char*指针,该指针指向的数据属于临时AnsiString,在您使用指针,从而表现出未定义的行为。复制AnsiString之前,请勿销毁它:

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

或者,根本不转换为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]);
}

但是,如果文本实际上包含原始字节的十六进制编码表示,则您需要解析文本以解码十六进制序列到实际字节,例如:

#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;
}

答案 1 :(得分:0)

编译器将编译制表符的十六进制值,但是我找不到转换它的函数-尽管Remy使用的是scanf。我将标签页更改为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 );
}
//---------------------------------------------------------------------------