|
|
Hi,
what is the easiest method to copy one byte array to another... but not all, only a part. Let say that we have :
byte[] a = new byte[]{1,2,3,4,5,6,7,8,9,10};<br />
byte[] b;
how to copy to array b bytes from array a but from offset 2 to 6 only.
I know that can use for loop but maybe there is better method?
Greetings
|
|
|
|
|
Take a look at the Array.Copy method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Application is compiled well but when run i got error like:
Could not load type 'Test.EF' from assembly ... because it contains an object field at offset 1 that is incorrectly aligned or overlapped by a non-object field.
<br />
[StructLayout(LayoutKind.Explicit)]<br />
public struct EF<br />
{<br />
<br />
[FieldOffset(0)]<br />
public byte clockStop;
[FieldOffset(1)]<br />
private byte[] cardExtendedSerialNumber;
[FieldOffset(8)]<br />
private byte[] cardApprovalNumber;
[FieldOffset(16)]<br />
private byte cardPersonaliserID;
[FieldOffset(17)]<br />
private byte[] embedderIcAssemblerId;
[FieldOffset(23)]<br />
private byte[] idIdentifier;
<br />
};<br />
Any idea how fix it?
Greetings
|
|
|
|
|
yarns wrote: [FieldOffset(1)]
private byte[] cardExtendedSerialNumber; //8
[FieldOffset(8)]
1 + 8 = 9
---
single minded; short sighted; long gone;
|
|
|
|
|
|
What do you mean exactly by "dont work"?
Do you get any error message?
What does your code look like now?
---
single minded; short sighted; long gone;
|
|
|
|
|
There probably are two problems:
1)
when you write byte[] you are declaring a reference type, you can think of it as a pointer
to some (yet unallocated) piece of memory. Such pointer needs 4 or 8 bytes of storage
(depending on you using Win32 or Win64) and independent of the amount of data you expect
your array to hold; also the pointer must be properly aligned (the offset
must be a multiple of 4, maybe of 8). Some of your arrays do not satisfy the alignment
requirment, and that explains the error code you are getting (its an align error,
not an overlap error).
2)
I suspect you really want your struct to hold some 25 bytes, and its layout to be
fixed (either by some hardware, or by a file format). If so, you must do it
differently; I see basically two possible approaches:
a) replace the struct by a single byte array, and take care of the offsets within your
code. This is the unstructured approach.
b) replace the struct by a combination of simple variables and other structs (which
again are limited to value types such as ints and value-only structs); and you could
and should teach those sub-structs how to interpret the data; could be something like (not correct, just indicative):
public struct string8 {
public byte b0, b1, b2, b3, b4, b5, b6, b7;
public string8(string s) {
b0=0; b1=0; b2=0; b3=0; b4=0; b5=0; b6=0; b7=0;
if (s==null) return;
if (s.Length>=1) b0=(byte)s[0];
if (s.Length>=2) b1=(byte)s[1];
if (s.Length>=3) b2=(byte)s[2];
if (s.Length>=4) b3=(byte)s[3];
if (s.Length>=5) b4=(byte)s[4];
if (s.Length>=6) b5=(byte)s[5];
if (s.Length>=7) b6=(byte)s[6];
if (s.Length>=8) b7=(byte)s[7];
}
public override string ToString() {
StringBuilder sb=new StringBuilder(8);
if (b0!=0) sb.Append((char)b0);
if (b1!=0) sb.Append((char)b1);
if (b2!=0) sb.Append((char)b2);
if (b3!=0) sb.Append((char)b3);
if (b4!=0) sb.Append((char)b4);
if (b5!=0) sb.Append((char)b5);
if (b6!=0) sb.Append((char)b6);
if (b7!=0) sb.Append((char)b7);
return sb.ToString();
}
}
[StructLayout(LayoutKind.Explicit)]
public struct EF {
[FieldOffset(0)] public byte clockStop;
[FieldOffset(1)] public string8 s1;
[FieldOffset(9)] public string8 s2;
}
public override void Run(int arg) {
EF e=new EF();
e.s1=new string8("jef");
e.s2=new string8("jan");
log("names= "+e.s1+" "+e.s2);
}
This code produces the line "names= jef jan"
BTW: the ToString() method for e.s1 and e.s2 is called implicitly, due to
the string+something expression
remark: there may be easier ways to do the constructor and the ToString(), but
I cannot immediately think of one.
Hope this helps.
Luc Pattyn
|
|
|
|
|
Well done
Its Great
Thank you
|
|
|
|
|
Hello everybody , i just have a compression algorithm written in ansi C i need to convert it into a .dll file to add its reference into .net studio , i did that like that
new project---->c++ projects--->MFC DLL File.
and i added its files and i wrote my own methods but when i tried to incorporate it into the .net studio it refused.
so is there any way to make it a dll file and add its reference to the .net studio.
second problem:
--------------
i have A .dll file i need to extract its methods
can i use it by [DLLImport("string to the file")]
when i do that it tells me the string assigned must be constant expression , here it means constant expression not only with "const" declaration but also no variable is assigned
i did that
const string path = Application.StartUpPath+"\\compression.dll";
[DllImport(path)]
public static extern void CompressData();
but it gives me an error
Please help me how can i get these methods out from the .dll file.
Human knowledge belongs to the world.
|
|
|
|
|
No, the string inside the DllImport() must be a constant, something like "kernel32.dll",
it can not be a variable.
Same is true for EntryPoint; it allows you to specify the exact name of the function
inside the DLL, but it too must be a double-quoted constant.
Luc Pattyn
|
|
|
|
|
Hi,
I have created a small software using C# in .NET2003. The software contains a access mdb file as a part. How do i package my software for deployment as an installer? Thanks in advance for any link, suggestion or instructions resolving this.
Thanks
|
|
|
|
|
Ship the .NET framework, your app and your MDB. I use a program called Advanced Installer, it's free. You need to ship the .NET framework seperately, however.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
With a Deployment project.
|
|
|
|
|
I have MDI form. MDI form contains toolstrip control. Toolstrip control contains menus so that I can call to the other forms
e.g.
client option
I can call client form by clicking the toolstrip client option
Client form act as child of MDI form
It works
Now client form contains one label. With the same functionality I want the forms appears under the MDI form that is MDI form can be display with child form .
It display but cannot capture the whole space of child form.
I am not using the maximize and minimize property
|
|
|
|
|
I'm executing SQL Server stored procedures from C# and I'm having problem figuring out a few things. My stored procedure have become pretty complex and I need to check whether the SP executed successfully or not. What should I do? Should i use a RAISEERROR in the SP, should i use RETURN <value> or should i use SELECT? Once i find out in my C# code that an error has occurred, i want to display the appropriate message to the user. How do I do this? Would i have to enclose the C# code to execute the SP in a try-catch block?
Thanks in advance guys!
|
|
|
|
|
I'd return an error code, and have it map to an enum in the C# for all possible errors, or success.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi,
what is the best method to reading and analizing binary files?
My file looks like:
3 bytes - tag type
2 bytes - tag lenght
x bytes - tag value
but in tag value can be many things like:
00 0F 00 - tag type
00 20 00 - tag lenght - 32 bytes
XX XX XX ... tag value where : first 16 bytes is a company name and last 16 bytes company address.
Here is full file structure: image
Any proposition?
Greetings
|
|
|
|
|
You obviously need a binary file class, and not a text class. In VS2005, File.ReadAllBytes should do to get the data into memory.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I'd use the .NET BinaryReader .
Read the first 5 Bytes, then read the N bytes of the tag value and so on.
Should be easy enough to read.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I have one main form.
there are 4-5 label control and i want to call forms with associated with that label control.
if you instead of label How can i used Linklabel control to call the form.
How can i call.
|
|
|
|
|
The linklabel has an onclick, you use that to open your new form. You can't create links to forms, you have to fake it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Is it possible to perform a trap or catch of an application that is forcibly killed from the Windows Task Manager or similar alternative, so that I can perform some late backing up of data just before the process disappears?
I wonder if Windows applications are as forgiving as Linux processes where I can make use of signal handlers.
Please advise, naturally it's in C# .NET 2.0, thanks in advance.
|
|
|
|
|
You should be able to achieve this by registering an event handler to the Application.Exit event or even the Form.FormClosing event.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Well... for that matter, I tried, but it only works if you exit the application normally. Clicking the close button on the form. But if you attempt to kill it from the Windows Task Manager, neither events will be handled. The application was forcibly killed off and nothing happens, not my event handler codes will run upon ending task. What I'm talking about is you pull our Windows Task Manager, go to processes tab, right-click on the process for the context menu, kill process.
|
|
|
|