Tatia Posted April 29, 2013 at 03:33 AM Report #505134 Posted April 29, 2013 at 03:33 AM Boas amigos poderiam me ajudar? Tenho um código na linguagem C Gostaria de ter esse mesmo código na linguagem VB.NET ou c# tenho preferência em VB.NET acredito que para vocês que sabem isto e bem fácil porque ele e bem pequeno. #pragma once #ifdef __cplusplus extern "C" { #endif typedef char s8; typedef unsigned char u8; typedef short s16; typedef unsigned short u16; typedef int s32; typedef unsigned int u32; #if defined(_WIN32) && defined(_MSC_VER) typedef __int64 s64; typedef unsigned __int64 u64; #else typedef long long int s64; typedef unsigned long long int u64; #endif #define BOOL int #define TRUE 1 #define FALSE 0 //Align. #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) //Bits <-> bytes conversion. #define BITS2BYTES(x) ((x) / 8) #define BYTES2BITS(x) ((x) * 8) //Endian swap for u16. #define _ES16(val) \ ((u16)(((((u16)val) & 0xff00) >> 8) | \ ((((u16)val) & 0x00ff) << 8))) //Endian swap for u32. #define _ES32(val) \ ((u32)(((((u32)val) & 0xff000000) >> 24) | \ ((((u32)val) & 0x00ff0000) >> 8 ) | \ ((((u32)val) & 0x0000ff00) << 8 ) | \ ((((u32)val) & 0x000000ff) << 24))) //Endian swap for u64. #define _ES64(val) \ ((u64)(((((u64)val) & 0xff00000000000000ull) >> 56) | \ ((((u64)val) & 0x00ff000000000000ull) >> 40) | \ ((((u64)val) & 0x0000ff0000000000ull) >> 24) | \ ((((u64)val) & 0x000000ff00000000ull) >> 8 ) | \ ((((u64)val) & 0x00000000ff000000ull) << 8 ) | \ ((((u64)val) & 0x0000000000ff0000ull) << 24) | \ ((((u64)val) & 0x000000000000ff00ull) << 40) | \ ((((u64)val) & 0x00000000000000ffull) << 56))) #ifdef __cplusplus } #endif
rezanov Posted April 29, 2013 at 10:11 AM Report #505151 Posted April 29, 2013 at 10:11 AM http://www.csharp-examples.net/reverse-bytes/ Ajuda ?
Tatia Posted April 29, 2013 at 10:00 PM Author Report #505237 Posted April 29, 2013 at 10:00 PM não esta a trabalhar aqui testas-te? Muito obrigado...
rezanov Posted May 2, 2013 at 02:06 PM Report #505589 Posted May 2, 2013 at 02:06 PM (edited) consegues deduzir o que pretendes com isto ? using System; using System.IO; using System.Collections.Generic; namespace FileOperationsSample { class TestBinaryIO { // if using console: csc /out:BinaryIO.exe BinaryIO.cs const string formatter = "{0,5}{1,17}{2,10}"; static void Main(string[] args) { string filename = @"c:\temp\test.data"; if(File.Exists(filename)) { Console.WriteLine(filename + " already exists!"); return; } FileStream fs = new FileStream(filename, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(fs); //write the numbers 12 to 29 (exclusive) - 2 bytes each number for(int i=12; i<29; i++) { bw.Write((UInt16)i); } bw.Close(); fs.Close(); //start reading the file fs = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Console.WriteLine(string.Format("length of the file (int): {0}",(int)br.BaseStream.Length)); //beginning of the file int fileposition = 0; //total length of the file int length = (int)br.BaseStream.Length; //Start from the origin br.BaseStream.Seek(fileposition, SeekOrigin.Begin); Console.WriteLine(string.Format("Little endian: {0}", BitConverter.IsLittleEndian)); int pos = 0; int index = 0; List<byte> lista = new List<byte>(); Console.WriteLine( formatter, "index", "array elements", "short" ); Console.WriteLine( formatter, "-----", "--------------", "-----" ); while(pos < length) { byte[] by = br.ReadBytes(2); //what is the value of the bytes i have read ? short value = BitConverter.ToInt16(by, index); Console.WriteLine( formatter, index, BitConverter.ToString( by, index, 2 ), value ); //if(BitConverter.IsLittleEndian) Array.Reverse(by); //what is the value of the bytes after reversing it ? value = BitConverter.ToInt16(by, index); Console.WriteLine( formatter, index, BitConverter.ToString( by, index, 2 ), value ); Console.WriteLine(); pos = pos + 2; } br.Close(); fs.Close(); } } } Edited May 2, 2013 at 02:06 PM by rezanov
Tatia Posted May 2, 2013 at 07:33 PM Author Report #505647 Posted May 2, 2013 at 07:33 PM Esse código e para fazer um byte swap de um ficheiro no formato bin. Exemplo File.bin (original este ficheiro tem um tamanho de 16.777.212 bytes) Original: E eu pretendia que ele fica-se assim.... Isso e só um exemplo pois eu pretendia mesmo era quando mandava abrir o ficheiro ele o le-se ja em byte reversed de modo a apresentar os dados correctos nas texbox... Outro exemplo que posso deixar e. Em hex temos os valores assim: 12 13 etc eu pretendo que ao ler o ficheiro ele converta assim. 13 12 etc Vou deixar um exemplo para tu mesmo veres como estou a fazer no VB, abres o projecto e depois vais abrir os ficheiros que tenho junto e vez o original e o pretendido para veres a diferença. eu pretendo e quando o projecto abre o arquivo original ele transforme ele em modo que humano possa ler. Download: Sample
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now