无法将BYTE转换为WORD?

编程入门 行业动态 更新时间:2024-10-27 16:26:48
本文介绍了无法将BYTE转换为WORD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个变量:char A和短B。我可以使用明确的大小写转换将A / B 从A转换为B,没有问题,例如B = short (A);。现在,我有两个变量:char T [6]和短A。 T有六个元素的数组。 。我希望捕获第一个元素和第二个 元素作为短字的两个字节。 问题是A仅捕获一个元素而不是两个元素。我看过机器语言,我发现C ++ 编译器选择错误的指令,它使用MOV EAX,BYTE PTR [T] 而不是MOV EAX,WORD PTR [T]。 有没有办法如何使用明确的 大小写转换来修复源代码中的错误?我尝试使用dynamic_cast<>,但结果相同。 以下是我的示例代码。 Bryan Parkoff int main(无效) { unsigned char T [6] = {" Bryan" }; //我选择了unsigned char for string 而不是char。 unsigned short A; A =无符号短(* T); //应该捕获Br 返回0; }

解决方案

Bryan Parkoff写道:

我有两个变量:" char A"和短B。我可以使用明确的大小写转换从A转换为B,没有问题,例如B = short (A);。 实际上,AFAIK,没有必要明确。隐式转换 应该可以正常工作: B = A; 现在,我有两个变量:" char T [6]"和短A。 T有一个由六个元素组成的数组。我希望捕获第一个元素和第二个元素作为短字的两个字节。问题是A和A。只捕获一个元素而不是两个元素。我看过机器语言,发现C ++ 编译器选择错误的指令,它使用MOV EAX,BYTE PTR [T] 而不是MOV EAX,WORD PTR [T]。有没有办法如何使用显式的大小写转换来修复源代码中的错误? 不,你需要一个算术(或位操作)表达式。 我试图使用dynamic_cast<>,但它有相同的结果。返回0; }

V

Bryan Parkoff写道:我有两个变量:" char A"和短B。我可以使用明确的大小写转换从A转换为B,没有问题,例如B = short (A);。现在,我有两个变量:char T [6]和短A。 T有一个由六个元素组成的数组。我希望捕获第一个元素和第二个元素作为短字的两个字节。问题是A和A。只捕获一个元素而不是两个元素。我看过机器语言,发现C ++ 编译器选择错误的指令,它使用MOV EAX,BYTE PTR [T] 而不是MOV EAX,WORD PTR [T]。有没有办法如何使用显式的大小写转换来修复源代码中的错误?我尝试使用dynamic_cast<>,但结果相同。以下是我的示例代码。 Bryan Parkoff int main(void) { unsigned char T [6] = {" Bryan" }; //我选择了unsigned char for string 而不是char。 unsigned short A; A = unsigned short(* T); //应该捕获Br 返回0; }

* T是无符号字符,所以你有相当于: unsigned char C =''B''; unsigned short A =无符号短(C); 为什么还能期待别的?

Bryan Parkoff写道:

我有两个变量:char A和短B。我可以使用明确的大小写转换从A转换为B,没有问题,例如B = short (A);。现在,我有两个变量:char T [6]和短A。 T有一个由六个元素组成的数组。我希望捕获第一个元素和第二个元素作为短字的两个字节。问题是A和A。只捕获一个元素而不是两个元素。我看过机器语言,发现C ++ 编译器选择错误的指令,它使用MOV EAX,BYTE PTR [T] 而不是MOV EAX,WORD PTR [T]。有没有办法如何使用显式的大小写转换来修复源代码中的错误?我尝试使用dynamic_cast<>,但结果相同。以下是我的示例代码。 Bryan Parkoff int main(void) { unsigned char T [6] = {" Bryan" }; //我选择了unsigned char for string 而不是char。 unsigned short A; A = unsigned short(* T); //应该捕获Br 返回0; }

好​​像你在想你所拥有的将开始复制 将指针值转换为short。它没有。 如果你想这样做,你可以使用memcpy()。 memcpy(& A,T,2 ); 但是,你必须确定'你想要的字节顺序和所有 那个。 如果你准确地解释了你想要做什么会有所帮助, 将两个字符复制成一个短片并不是一个典型的 操作。 Brian

I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T[6]" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language and I discovered that C++ Compiler selects the wrong instruction which it uses MOV EAX, BYTE PTR [T] instead of MOV EAX, WORD PTR [T]. Is there a way how I can fix an error in my source code using explicit case conversion? I tried to use dynamic_cast<>, but it has the same result. Here is my example code below. Bryan Parkoff int main(void) { unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string instead of char. unsigned short A; A = unsigned short (*T); // Should capture "Br" return 0; }

解决方案

Bryan Parkoff wrote:

I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Actually, AFAIK, there is no need to be explicit. Implicit conversion should work just fine: B = A; Right now, I have two variables: "char T[6]" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language and I discovered that C++ Compiler selects the wrong instruction which it uses MOV EAX, BYTE PTR [T] instead of MOV EAX, WORD PTR [T]. Is there a way how I can fix an error in my source code using explicit case conversion? No, you need an arithmetic (or bit manipulation) expression. I tried to use dynamic_cast<>, but it has the same result. Here is my example code below. Bryan Parkoff int main(void) { unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string instead of char. unsigned short A; A = unsigned short (*T); // Should capture "Br" Why should it? You say here, essentially, A = unsigned short(T[0]); so it does as you ask, only takes the first one. You should do something like A = (T[0] << CHAR_BIT) | T[1]; (or vice versa depending on where in A you want the ''B'' and where the ''r'') return 0; }

V

Bryan Parkoff wrote:

I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T[6]" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language and I discovered that C++ Compiler selects the wrong instruction which it uses MOV EAX, BYTE PTR [T] instead of MOV EAX, WORD PTR [T]. Is there a way how I can fix an error in my source code using explicit case conversion? I tried to use dynamic_cast<>, but it has the same result. Here is my example code below. Bryan Parkoff int main(void) { unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string instead of char. unsigned short A; A = unsigned short (*T); // Should capture "Br" return 0; }

*T is an unsigned char, so you have the equivalent of: unsigned char C = ''B''; unsigned short A = unsigned short(C); Why would you expect anything else?

Bryan Parkoff wrote:

I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T[6]" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language and I discovered that C++ Compiler selects the wrong instruction which it uses MOV EAX, BYTE PTR [T] instead of MOV EAX, WORD PTR [T]. Is there a way how I can fix an error in my source code using explicit case conversion? I tried to use dynamic_cast<>, but it has the same result. Here is my example code below. Bryan Parkoff int main(void) { unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string instead of char. unsigned short A; A = unsigned short (*T); // Should capture "Br" return 0; }

Seems like you are thinking that what you have will start copying at the pointer value into the short. It doesn''t. If you did want to do that, you could use memcpy(). memcpy (&A, T, 2); However, you have to be sure that''s the byte order you want and all that. It would be helpful if you explained exactly what you are trying to do, as copying two characters into a short isn''t all that typical of an operation. Brian

更多推荐

无法将BYTE转换为WORD?

本文发布于:2023-11-10 02:54:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   BYTE   WORD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!