FreewareFire Posted May 23, 2003 Posted May 23, 2003 Hi, Question: How do i convert an int value to an byte array? Then i need to use it like " byte[0] " ... " [byte[1] " ans so on. Thank you! Please a simple way!!! Or with an explanation? Better a little example! :confused: Quote
Madz Posted May 29, 2003 Posted May 29, 2003 (edited) I m not sure abou it , but some where i found this code please check it in this it has converted from String to Byte. Imports System Imports System.IO Imports System.Security.Cryptography Imports System.Text Class FileEncrypt Public Shared Function ConvertStringToByteArray(s As [string]) As [byte]() Return (New UnicodeEncoding()).GetBytes(s) End Function 'ConvertStringToByteArray Public Shared Sub Main() Dim fs As New FileStream("EncryptedFile.txt", FileMode.Create, FileAccess.Write) 'Creating a file stream Console.WriteLine("Enter Some Text to be stored in encrypted file:") Dim strinput As [string] = Console.ReadLine() Dim bytearrayinput As [byte]() = ConvertStringToByteArray(strinput) 'DES instance with random key Dim des As New DESCryptoServiceProvider() 'create DES Encryptor from this instance Dim desencrypt As ICryptoTransform = des.CreateEncryptor() 'Create Crypto Stream that transforms file stream using des encryption Dim cryptostream As New CryptoStream(fs, desencrypt, CryptoStreamMode.Write) 'write out DES encrypted file cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) cryptostream.Close() 'create file stream to read encrypted file back Dim fsread As New FileStream("EncryptedFile.txt", FileMode.Open, FileAccess.Read) 'create DES Decryptor from our des instance Dim desdecrypt As ICryptoTransform = des.CreateDecryptor() 'create crypto stream set to read and do a des decryption transform on incoming bytes Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read) 'print out the contents of the decrypted file Console.WriteLine(New StreamReader(cryptostreamDecr, New UnicodeEncoding()).ReadToEnd()) Console.WriteLine () Console.WriteLine ("Press Enter to continue...") Console.ReadLine() End Sub 'Main End Class 'FileEncrypt [edit]Funny looking C#[/edit] Edited May 30, 2003 by Squirm Quote The one and only Dr. Madz eee-m@il
FreewareFire Posted May 29, 2003 Author Posted May 29, 2003 Thanks But still found the answer - by myself: for exmp. : byte[] memory = BitConverter.GetBytes(Convert.ToInt16(12)); This converts first the value 12 into Int16 format (2 Bytes !). Then retrieves from this Int16 value with the GetBytes function the bytes and stores them into varibale "memory". to read each byte of this value you still need to do follow: ... = memory[0]; //This puts the first byte of memory to any place you definded before the "=" ! to access the 2 Byte: memory[1] - and so on! But thanks! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.