Write String as Byte

Lifestyle

Member
Joined
Jan 28, 2010
Messages
6
Programming Experience
1-3
I've got a problem saving asci Values to File as Byte.

I must save hex values to file, for this case i used the following line:
fs.WriteByte(&H2D)

This works fine, but i need to do this autmaticly. My Sourcestring is 2995030.

Tried to do something like &H(Hex(str)), but this failed.

Any ideas ??
 
Edit: My first example wouldn't have worked, because your value is over the max value of a byte.

However:
BitConverter.GetBytes(Integer.Parse("2995030")) will return a byte array and then you can do fs.Write(ByteArrayItem) for each byte in the array.

(don't have visual basic atm so I didn't make the exact code, but you should be able to figure it out.)
 
Last edited:
Hex is actually a string representation of a number, so what you want (need) to do is not clear. Whether you write the string or the string bytes is exactly the same, but a number and a string is not the same. Perhaps you can explain some more? Btw, here is some conversions of the number string you mentioned (you did say "string"):
VB.NET:
Dim text As String = "2995030"
Dim number As Integer = CInt(text)
Dim hex As String = number.ToString("X")
 
I need to write an audio header.

the number i mentioned is an integer which should be written into the header. I need to write this string as byte to file.

&H2B is only a represenation of the byte which will bew ritten right ? Cause vb studio told me the byte is 43.
 
hi,

i've already tried it:
VB.NET:
dim encoding as new system.text.asciiencoding()
dim bla as byte() = encoding.getbytes("2995030")

Results in the following bytes: 50 57 57 53 48 51 48
but the result must be: 45 179 86

I think its the ascii encoding which is the fault, but i didn't know any other solution
 
there is another odd behaviour,....

i've writting the same string 4 times into that file (only for testing)
VB.NET:
for i as integer = cint(part1.getlength(0) / 2 -1) to 0 step -1
  fs.writebyte(part(i))
next
for i as integer = cint(part1.getlength(0) / 2 -1) to 0 step -1
  fs.writebyte(part(i))
next
for i as integer = cint(part1.getlength(0) / 2 -1) to 0 step -1
  fs.writebyte(part(i))
next
for i as integer = cint(part1.getlength(0) / 2 -1) to 0 step -1
  fs.writebyte(part(i))
next

The Result in the file as hex is:
00 86 2A AC 00 86 2A AC 00 86 2A AC (00) 86 2A AC

The Odd thing is, the marked value is missing.
 
Back
Top