无法将BYTE转换为WORD? [英] Can't Convert BYTE to WORD?

查看:97
本文介绍了无法将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<>,但它有相同的结果。<以下是我的示例代码。

Bryan Parkoff

int main(无效)
{
unsigned char T [6] = { "布赖恩" }; //我选择了unsigned char for string
而不是char。
unsigned short A;

A = unsigned short(* T); //应该捕获Br


为什么要这样?你在这里说,基本上,


A =无符号短(T [0]);


所以它按照你的要求,只需要第一个。你应该做点什么

就像


A =(T [0]<< CHAR_BIT)| T [1];


(反之亦然,取决于你想要'B'的位置和''r''的位置)

>返回0;
}




V


Bryan Parkoff写道:< blockquote class =post_quotes>我有两个变量:" 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆