- This topic has 3 replies, 3 voices, and was last updated 5 years, 9 months ago by .
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.
Integrated Application Platform
Integrated Application Platform › Forums › General › Writing a file with hex values
Hi,
I was trying to write unicode hex values directly into a file using Suneido.
When I try
File("test.txt", mode = "ab")
{ |f|
f.Write(0x0D06)}
the number 3334 gets written. If I use
f.Write(Pack(0x0D06))
then the 0D06 is written but preceeded by 0381
How can I get just the hexvalue written in the file?
Thanks,
ajith
I don’t think there is an existing function to display hex value string
so we need something like the following code
n = 0x0D06
map = ‘0123456789ABCD’
s = ”
while (n >= 1)
{
c = n % 16
s = map[c] $ s
n = (n – c) / 16
}
s.LeftFill(4, ‘0’)
we probably could add to Numbers as well
f.Write(0x0D06) converts the hex number to decimal number automatically, the future version of cSuneido will not convert automatically anymore, and will throw error instead.
The trick is to write a string containing the bytes you want to output.
e.g. f.Write('\x0d\x06')
Note: You need to get the byte order correct i.e. it might need to be \x06\x0d
The upcoming change in Suneido is that it will not automatically convert strings to number e.g. for arithmetic. However, literal numbers like 0x0D06 will still be handled. 0x0d06 and 3334 are just different representations for the same underlying number.
Thanks both of you for your replies. I tried what Andrew suggested and it is working well.
Thanks
ajith