相关疑难解决方法(0)

将std :: string或string ^转换为c ++/cli中的字节数组

我知道这是一个经常被问到的问题,我没有明确的答案将std :: string或String ^转换为字节数组,以便写入流进行tcp通信.

这就是我尝试过的

bool CTcpCommunication::WriteBytes(const std::string& rdatastr)
{
  bool retVal = false;

  try
  {
    if (static_cast<NetworkStream^>(stream) != nullptr)
    {
      array<Byte>^data = System::Text::Encoding::ASCII->GetBytes(rdatastr); 
      stream->Write( data, 0, data->Length ); 
    }
  }
  catch(Exception^)
  {
    // Ignore, just return false
  }
  return retVal;
}
Run Code Online (Sandbox Code Playgroud)

我知道这里GetBytes不会工作,我也检查了编组选项,将std:string转换为.NET String但是没有发现任何.有人帮我解决这个问题.

.net sockets string c++-cli tcpclient

1
推荐指数
1
解决办法
8164
查看次数

将Pascal中的等效代码写入代码C#

我正在尝试将pascal中的此代码转换为c#.没有编译错误,并且该方法适用于短字符串(4或5个字符).所有方法和函数都是等价的,但我的代码抛出了这个异常:

System.OverflowException:对于字符,值太大或太小.

这是StackTrace:

在C:\ Users\TRS\Documents\Visual Studio 2013\Projects\ConsoleApplication3 \中的ConsoleApplication3.Program.Encrypt(布尔加密,字符串字,Int32 startKey,Int32 multKey,Int32 addKey)中的System.Convert.ToChar(Int32值)中ConsoleApplication3\Program.cs:第29行.

这是Pascal代码:

function TGenericsF.Encrypter(Encrypt: WordBool; Source: AnsiString;
  StartKey, MultKey, AddKey: Integer): AnsiString;
 {$R-} {$Q-}
 var Counter: LongInt;
   S: AnsiString;
   Ret: AnsiString;
   begin
     S := Source;
     Ret := '';
     for Counter := 1 to Length(S) do
     begin
       if Encrypt then
        begin
         Ret := Ret + AnsiChar(Ord(S[Counter]) xor (StartKey shr 8));
         StartKey := (Ord(Ret[Counter]) + StartKey) * MultKey + AddKey;
        end
     else
       begin
         Ret := Ret + AnsiChar(Ord(S[Counter]) xor (StartKey …
Run Code Online (Sandbox Code Playgroud)

.net c# delphi

1
推荐指数
1
解决办法
989
查看次数

使用文件名中包含多个连续空格的文件

在我的代码的某些部分,我检查文件是否存在然后打开它.

一名员工遇到包含多个空格字符的文件名问题.

我查了一下,这是真的.这是我的代码片段:

string filePath = Path.Combine(helper.MillTestReportPath, fileName);

// Ouverture du fichier
if (File.Exists(filePath))
{
    Process.Start(filePath);
}
else
{
    MessageBox.Show("Le fichier n'existe pas!", "Fichier introuvable", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Run Code Online (Sandbox Code Playgroud)

几乎每个文件都可以找到所有内容但是当一个文件(例如"SPAM CERTS SO 94318099 PO 10610.msg")包含多个空格时,我会使用File.Exists得到错误,即使我直接尝试运行Process.Start它失败...

关于我如何解决这个问题的任何想法?

非常感谢!

.net c#

0
推荐指数
1
解决办法
716
查看次数

在 C# 中将字节数组转换为字符串并返回

我正在将文件读入字节数组并将字节数组转换为字符串以传递到方法中(我无法传递字节数组本身),并且在函数定义中我将字符串重新转换为字节数组。但两个字节数组(转换前后不同)

我正在使用以下试点代码来测试字节数组是否相同。

byte[] bytes = File.ReadAllBytes(@"C:\a.jpg");
 string encoded = Convert.ToBase64String(bytes);
byte[] bytes1 = Encoding.ASCII.GetBytes(encoded);
Run Code Online (Sandbox Code Playgroud)

当我在 api 调用中使用 bytes 时,它会成功,而当我使用 bytes1 时,它会引发异常。请告诉我如何安全地将字节数组转换为字符串并返回,以使两个数组重新排列相同。

c#

0
推荐指数
1
解决办法
3759
查看次数

将字符串转换为byte []以获取套接字

我正在用c#编写一个简单的ftp客户端.
我不是c#的专业人士.有没有办法将字符串转换为byte []并将其写入套接字?
例如,为了引入用户名,这是套接字内容:

5553455220736f726f7573680d0a
Run Code Online (Sandbox Code Playgroud)

和ASCII等价物是:

USER soroush
Run Code Online (Sandbox Code Playgroud)

我想要一个转换字符串的方法.像这样的东西:

public byte[] getByte(string str)
{
    byte[] ret;
    //some code here
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

c# sockets string ftp byte

-4
推荐指数
1
解决办法
2510
查看次数

标签 统计

c# ×4

.net ×3

sockets ×2

string ×2

byte ×1

c++-cli ×1

delphi ×1

ftp ×1

tcpclient ×1