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;
} 13 7922
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
"Default User" <de***********@ yahoo.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
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,
Thank you for the information. I am sure that memcpy() works, but I
want to use two x86 instruction that uses WORD instead of BYTE, but C++
Compiler only assigns BYTE instead of WORD. It is the way how C++ Compiler
works. I don't know if there is no solution so I am forced to use __asm
function. I do not wish to use left shift to move first byte to the left
before use "or" to capture second byte so two bytes becomes word. SHIFT and
ROTATE are hurt on Intel Pentium 4 because of eating about 7 clock cycles
rather than one clock cycle.
Variable: T is defined in BYTE ARRAY, but I force to tell C++ Compiler
to capture two bytes from BYTE ARRAY. Please look at my C++ source code
with my comment below.
// Example 1
int main(void)
{
unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string
instead of char.
unsigned short A; // C++ Compiler assigns "movzx EAX, BYTE PTR [T]"
because T is defined as BYTE ARRAY.
A = unsigned short (*T); // Should capture "Br"
return 0;
}
// Example 2
int main(void)
{
unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string
instead of char.
unsigned short A; // C++ Compiler assigns "movzx EAX, BYTE PTR [T]"
because T is defined as BYTE ARRAY.
// A = unsigned short (*T); // Should capture "Br"
__asm
{
movzx EAX, WORD PTR [T] // Remove "movzx EAX, BYTE PTR [T]" from C++
Compiler
mov WORD PTR [A], AX
}
return 0;
}
Bryan Parkoff
Bryan Parkoff wrote: [redacted]
1. How a specific compiler generates cod is OT.
2. 80x86 ASM is OT (the __asm keyword)
3. To do what you want, you need to drop into the realm of UB
(specifically, cast &T[0] to an unsigned short *, and dererence that.
4. What, specifically, are you attempting to do, where memcpy() would be
insufficient (whereas it is defined behavior and portable, save for
endian issues)?
"Bryan Parkoff" <no****@nospam. com> wrote in message news:w8******** ******@newssvr3 0.news.prodigy. com...
A = *((unsigned short *)T);
Todd
"Todd Brylski" <tb******@yyaah hoooo.com> wrote in message
news:yr******** ***********@tor nado.rdc-kc.rr.com... "Bryan Parkoff" <no****@nospam. com> wrote in message news:w8******** ******@newssvr3 0.news.prodigy. com...
A = *((unsigned short *)T);
Todd,
You got the right answer. Thank you very much. It helps me a lot to
save performance.
Bryan Parkoff
Bryan Parkoff wrote: "Todd Brylski": "Bryan Parkoff" <no****@nospam. com> wrote:
A = *((unsigned short *)T);
Note that those brackets are unnecessary:
A = *(unsigned short *)T;
You got the right answer. Thank you very much. It helps me a lot to save performance.
I guess that depends what you mean by 'performance'. This code
is non-portable, eg. it may crash the program on Sun hardware.
Try this one:
memcpy(&A, T, sizeof A);
Bryan Parkoff wrote: 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; }
I think what you're looking for might have been:
A = *((unsigned short *)T);
But this is naughty as it assumes that sizeof(short) == 2, and that your
machine architecture is little-endian.
--
Mike Smith This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vincent |
last post by:
Hi all,
I want to convert a char (binary) to an unsigned long. How can I do
this?
Thanks,
Vincent
|
by: windandwaves |
last post by:
Hi folk
Create two tables:
1. SOURCE
2. SOURCE_CONVERSION
give them two fields:
1. ID = autonumber primary key
|
by: Kristian Nybo |
last post by:
Hi,
I'm writing a simple image file exporter as part of a school project,
and I would like to write completely platform-independent code if at all
possible. The problem I've run into is that apparently there's no
guarantee that a particular implementation will have a data type that is
exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct me if I'm wrong, but I assume that if CHAR_BIT happens to be,
say, 9, writing...
|
by: Christopher Beltran |
last post by:
I am currently trying to replace certain strings, not
single characters, with other strings inside a word
document which is then sent to a browser as a binary
file. Right now, I read in the word file, convert the
FileStream into a string using Unicode encoding, then do a
replace, then convert the string back to a byte using
Unicode encoding which i then Response.WriteBinary(bytes)
to the browser. This works fine although the actual...
|
by: Mel Weaver |
last post by:
Hello,
I'm trying to convert these to functions to c#
const
C1 = 52845;
C2 = 22719;
function Encrypt(const S: String; Key: Word): String;
var
I: byte;
| |
by: charliewest |
last post by:
Hello -
I have images saved in my SQL SERVER 2000 database. Using ASP.NET (C#) is
there any way to temporarily save an image to a session object, and after
running some other operations, later retrieve the image from the session
object, convert it back to an image, and re-save it to the database?
Thanks?
|
by: Charles Law |
last post by:
I thought this had come up before, but I now cannot find it.
I have a byte array, such as
Dim a() As Byte = {1, 2, 3, 4}
I want to convert this to an Int32 = 01020304 (hex).
If I use BitConverter, I get 04030201.
|
by: Bob Homes |
last post by:
In VB6, foreground and background colors of controls had to be assigned a
single number. If you knew the RGB values for the color, you still had to
convert them into the single number accepatable to the VB6 controls.
In VB.NET, you can't set colors that way anymore, now you have to use a
"color". There is a way to convert RGB values to a "color", using the
Color.FromARGB method. But there doens't seem to be a way to convert the old...
|
by: Franky |
last post by:
I have a Command Prompt window open and select all the characters and copy
them to the clipboard.
I then read them from the clipboard
str = CType(DataO.GetData(DataFormats.OemText, False), String)
and try to convert them to unicode
Dim InEncoding As Encoding = Encoding.GetEncoding(437)
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |