none
Write hex in little endian RRS feed

  • Question

  • Hello i need to write hex to an file.

    Im using this to read in big endian/little endian

    Dim beBOM As New System.Text.UnicodeEncoding(True, True)
            Dim littleEndian As Long
            Dim bigEndian As Long
            Using fs As New IO.FileStream("C:\Temp\theFile.bin", IO.FileMode.Open),
            br As New IO.BinaryReader(fs, beBOM)
                fs.Position = &H12
                Dim startIdx As Integer = 0
                Dim b() As Byte = br.ReadBytes(4)
                littleEndian = BitConverter.ToInt32(b, startIdx)
                bigEndian = BitConverter.ToInt32(b.Reverse.ToArray, startIdx)
            End Using
    
            Numericupdown1.value = littleEndian.ToString
            MessageBox.Show(bigEndian.ToString()) 

    In this case littleEndian is showed in Numericupdown1

    My target is change the value and write again to file but this time in hex. 

    Thanks

    Tuesday, September 17, 2019 12:32 AM

Answers

  • Hi,

    My code writes bin files in little endian  and big endian respectively.

     Using fs As New IO.FileStream("C:\Temp\theFile.bin", IO.FileMode.Open)
                Using sw As New IO.StreamWriter(fs)
                    Dim x As Integer = 0
                    Dim s As String = Nothing
                    Dim b As Byte() = BitConverter.GetBytes(x)
                    'little endian
                    s = BitConverter.ToString(b)
                    sw.WriteLine(s)
                    Array.Reverse(b)
                    'big endian
                    s = BitConverter.ToString(b)
                    sw.WriteLine(s)
                End Using
            End Using

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by extream87 Friday, September 20, 2019 8:12 AM
    Tuesday, September 17, 2019 2:30 AM
    Moderator

All replies

  • Hi,

    My code writes bin files in little endian  and big endian respectively.

     Using fs As New IO.FileStream("C:\Temp\theFile.bin", IO.FileMode.Open)
                Using sw As New IO.StreamWriter(fs)
                    Dim x As Integer = 0
                    Dim s As String = Nothing
                    Dim b As Byte() = BitConverter.GetBytes(x)
                    'little endian
                    s = BitConverter.ToString(b)
                    sw.WriteLine(s)
                    Array.Reverse(b)
                    'big endian
                    s = BitConverter.ToString(b)
                    sw.WriteLine(s)
                End Using
            End Using

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by extream87 Friday, September 20, 2019 8:12 AM
    Tuesday, September 17, 2019 2:30 AM
    Moderator
  • You cannot write hex in another way than as a string.

    Hexadecimal is a way to represent for humans easy bytes (8 bits) separated as 2 digits with numbers as characters from 0 to 15. 

    What you use (are able to address) in memory are words. For instance a 64 bit computer a word (the bits) are 64 bits long which are coded in Unicode as it is about characters. 

    However, a 64 bit word can be human represented as 8 bytes in hexadecimal. 




    Success
    Cor


    • Edited by Cor Ligthert Tuesday, September 17, 2019 7:25 AM
    Tuesday, September 17, 2019 7:24 AM
  • Your code is writing like text and not like bytes.

    Tuesday, September 17, 2019 9:14 AM
  • Your code is writing like text and not like bytes.

    Hi,

    Do you want to write this way?

      Dim j = 0
                For i = 0 To (b.Length - 1) / 2 - 2
                    fs.Position = j
                    fs.Write(b, j, 2)
                    j += 3
    
                Next

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 18, 2019 2:34 AM
    Moderator
  • Hello Alex Li-MSFT yes seems to be it. Is not binary right?

    Your last code make the convertion like i want 87 F4 0D 00 but writes in text not in bytes.

    Thanks for your help. 

    • Edited by extream87 Wednesday, September 18, 2019 11:37 AM
    Wednesday, September 18, 2019 6:13 AM
  • Hello Alex Li-MSFT yes seems to be it. Is not binary right?

    Your last code make the convertion like i want 87 F4 0D 00 but writes in text not in bytes.

    Thanks for your help. 

    Read your original question, that is why I made my Reply. Alex did exact as you did ask. Could not have done better. 

    Success
    Cor

    Wednesday, September 18, 2019 2:43 PM
  • Hello im not english maybe i dont have explained me very well.

    Ive an solution but maybe is not the best way... 

    Using fs As New IO.FileStream("C:\Temp\theFile.bin", FileMode.Open, FileAccess.ReadWrite) Using sw As New IO.StreamWriter(fs) Dim x As Integer = Numericupdown1.Value Dim s As String = Nothing Dim b As Byte() = BitConverter.GetBytes(x) 'little endian s = BitConverter.ToString(b) Dim hex = s 'Array.Reverse(b) 'big endian 's = BitConverter.ToString(b) 'sw.WriteLine(s) End Using

    Dim bytes = Enumerable.Range(0, hex.Length \ 2 - 1).Select(Function(n) Convert.ToByte(hex.Substring(n * 2, 2), 16)).ToArray() fs.Position = &H12 fs.Write(bytes, 0, bytes.Length) End Using









    • Edited by extream87 Wednesday, September 18, 2019 3:39 PM
    Wednesday, September 18, 2019 3:31 PM
  • Hi,

    Do you resolve the issue?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 5:25 AM
    Moderator
  • Yes thank you so much mate. 
    Friday, September 20, 2019 8:12 AM