简体   繁体   English

CS1503:参数 1:无法从 'int' 转换为 'byte' C#

[英]CS1503: Argument 1: cannot convert from 'int' to 'byte' C#

private byte[] ParsePatternString(string szPattern)
{
  List<byte> list = new List<byte>();
  foreach ( string text in szPattern.Split(new char[] { ' ' }) )
  {
    list.Add((text == "?") ? 0 : Convert.ToByte(text, 16));
  }
  return list.ToArray();
}

The error happens on list.Add((text == "?")? 0: Convert.ToByte(text, 16));错误发生在list.Add((text == "?")? 0: Convert.ToByte(text, 16)); What I am wondering is how what is creating the error, because based on the other questions about a similar issue I tried and it seems to not have worked.我想知道是什么造成了错误,因为基于其他关于我尝试过的类似问题的问题,它似乎没有奏效。

0 is an int literal. 0是一个int字面量。 Just like C, there's no way to specify an integer literal smaller than int .就像 C 一样,无法指定小于intinteger 文字 The type of a conditional operator is the common type of the two values, thus your expression results in an int expression and can't be passed to a byte argument. 条件运算符的类型是两个值的通用类型,因此您的表达式会生成一个int表达式,并且不能传递给byte参数。 You need to cast the expression to byte using either of the following您需要使用以下任一方法将表达式转换为byte

list.Add(text == "?" ? (byte)0 : Convert.ToByte(text, 16));
list.Add((byte)(text == "?" ? 0 : Convert.ToByte(text, 16)));

You could use byte.MinValue in this case:在这种情况下,您可以使用byte.MinValue

private byte[] ParsePatternString(string szPattern)
{
    List<byte> list = new List<byte>();
    foreach ( string text in szPattern.Split(new char[] { ' ' }) )
    {
        list.Add(text == "?" ? byte.MinValue : Convert.ToByte(text, 16));
    }
    return list.ToArray();
}

I have actually never used CSharp before but I think the problem might be that it reads 0 as an int and not a Boolean, and doesn't automatically convert it.实际上我以前从未使用过 CSharp,但我认为问题可能在于它将 0 读取为 int 而不是 Boolean,并且不会自动转换它。 From what I'm reading about it you need to add an explicit cast like this:根据我正在阅读的内容,您需要添加这样的显式转换:

list.Add((text == "?") ? (byte) 0 : Convert.ToByte(text, 16));

暂无
暂无

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

相关问题 C# - CS1503 - 参数 1:无法从“字符串”转换为“整数” - C# - CS1503 - Argument 1: cannot convert from 'string' to 'int' C#无法从void转换为bool - CS1503 - C# Cannot convert from void to bool - CS1503 CS1503 C#参数1:无法从“ System.IO.Stream”转换为“ char []” - CS1503 C# Argument 1: cannot convert from 'System.IO.Stream' to 'char[]' CS1503:参数 2:无法从 &#39;System.Action[*,*,*] 转换为 System.Action[]&#39; C# 2022 - CS1503: Argument 2: Cannot convert from 'System.Action[*,*,*] to System.Action[]' C# 2022 C# 错误 CS1503 参数 1:无法从“字符串”转换为“字符” - C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character' CS1503 C# 参数 1:无法从“字符串”转换为“System.Data.SqlClient.SqlCommand” - CS1503 C# Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand' CS1503 参数 1:无法从 'string' 转换为 'string[*,*]' - CS1503 Argument1: cannot convert from 'string' to 'string[*,*]' 出现错误 CS1503:“参数 1:无法从 'System.Diagnostics,PerformanceCounter' 转换为 'int' - Getting Error CS1503: "Argument 1: Cannot convert from 'System.Diagnostics,PerformanceCounter' to 'int' CS1503 C#参数1:无法从“ Project.Vector2”转换为“ System.Predicate” <Project.Vector2> ” - CS1503 C# Argument 1: cannot convert from “Project.Vector2” to “System.Predicate<Project.Vector2>” CS1503:参数 3:无法从“double”转换为“UnityEngine.Quaternion”帮助! 统一二维 c# - CS1503: Argument 3: cannot convert from 'double' to 'UnityEngine.Quaternion' HELP! unity 2D c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM