Hi,
I just discovered Nim and already doing little toy projects with it to get used to it. Love it.
I am playing with networking. But reading from raw sockets, I need to cast the string to a tuple which represent the headers fields. That would enable me to select any field of the ethernet/ip/udp frame more easily.
I have:
type
Raw_Frame = tuple
mac_dest: array[6,char]
mac_src: array[6,char]
var t : ref Raw_Frame
var data_coming_from_socket : string = "123456789123" #Imagine that the string comes from recv
t = cast[ref Raw_Frame](addr c)
echo("hi ", t.mac_dest,t.mac_src)
nim
I want t.mac_dest = ['1','2','3','4','5','6'] and t.mac_src = ['7','8','9','1','2','3'] as an array of char Then, different types will follow but I wanted my question to remain concise.
Ultimately, I would cast the string to a tuple (I don't want objects, too much overhead) In C, you just cast your array of uint8 to a structure pointer and call it a day. Sure Nim does that too.
I understood that string is actually a pointer but not too sure. If you have a better idea as to how to do it the Nim way, please help!
Thanks,
I understand my mistake.
t = cast[ref Raw_Frame](addr c)
should be
t = cast[ref Raw_Frame](addr c[0])
Thanks Stefan,
I will copy the string on the heap to avoid it to be overwritten and cast it to a Raw_Frame ptr.
Except if I miss something important and dangerous in Nim, it is the way to go for me. Thanks!