Results 1 to 5 of 5

Thread: When reading byte, returning "07" instead of "7" - Byte editing

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2020
    Posts
    34

    When reading byte, returning "07" instead of "7" - Byte editing

    When reading a byte from a offset the software returns the byte value without the first zero "0" for instance if the byte return should be: "02" it returns "2". Can I make it so it returns the full "02" instead of cutting off the zero at the beginning ?
    For instance:

    Code:
    Dim i as Integer
    Dim FileToRead Byte() = File.ReadAllBytes(C:\......file.bin)
    Dim ResultString as String = CByte(FileToRead(i))
    debug.write(ResultString)
    That's only a representation of what the code looks like, but you guys get the point. I need the full byte in return, without cutting out the zero if it is in the beginning.
    Last edited by vargaperformance; Apr 12th, 2021 at 09:11 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: When reading byte, returning "0" instead of "00" - Byte editing

    There is no "cutting off" of leading zeroes because there are no leading zeroes to start with. A number is just a number. Leading zeroes is a way to represent a number when written. That means that, if you expect to see a leading zero, you need to add one yourself. You presumable want a hexadecimal representation of the byte as well, not a decimal representation. Bytes range in value from 0 to 255 in decimal, which is &H0 to &HFF (VB syntax) in hexadecimal.

    For a start, what's the point of using CByte on a value that you just got from a Byte array? Do you often find the need to convert things to what they already are? Just get your byte and convert it to a two-digit, hexadecimal String:
    vb.net Code:
    1. Dim ResultString as String = FileToRead(i).ToString("X2")
    The "X" format specifier means upper-case hexadecimal digit. If, for example, your Byte value is 12, that code will produce "0C".

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2020
    Posts
    34

    Re: When reading byte, returning "0" instead of "00" - Byte editing

    See the picture. I need to read that byte as "07" and not as "7". There are more bytes to be read but the point is I need to return it as a two digit value rather than one. (with a zero in the beginning).
    And for the purpose of my software I need to use CByte or some other function that can fetch a byte from a decimal adress like on the picture.
    Attached Images Attached Images  
    Last edited by vargaperformance; Apr 12th, 2021 at 09:10 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: When reading byte, returning "0" instead of "00" - Byte editing

    Quote Originally Posted by vargaperformance View Post
    See the picture. I need to read that byte as "07" and not as "7". There are more bytes to be read but the point is I need to return it as a two digit value rather than one. (with a zero in the beginning).
    Why are you repeating what you have already said when I have already told you how to do it? You stated a problem and I gave you the solution. Use it.
    Quote Originally Posted by vargaperformance View Post
    And for the purpose of my software I need to use CByte or some other function that can fetch a byte from a decimal adress like on the picture.
    No you don't. You probably ought to learn the basics first. CByte is used to convert a value that is not a Byte to a Byte. You have a Byte array and you are indexing it to get the element at that index. What exactly do you think an element of a Byte array is if not a Byte? You don't need to convert a Byte to a Byte so you don't need to use CByte.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: When reading byte, returning "07" instead of "7" - Byte editing

    Numbers, such as 7, don’t have leading zeros. Numbers expressed as Strings can have leading zeros

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width