I have a situation where I need to convert a number (a 32 bit integer) to a
4 byte string - i.e., the internal representation of that 32 bit number as 4 consecutive bytes. This is so that I can pass (the address of) that string to a low-level routine that wants (basically) an "int *" value.
I managed to get it working, using the following function:
# This assumes 32 bit ints on a little-endian architecture.
# Call as: str = encode(number)
function encode(n, i,s) {
s = sprintf("%c",n)
for (i=1; i<4; i++)
s = s sprintf("%c",rshift(n,i*8))
return s
}
This works, but I'm wondering if there is a better/more efficient/cuter way to do it. Please discuss.
Note, BTW, that I have verified that when you printf with %c, it only uses the low 8 bits of the number you pass in. So, you don't need to do any "AND"ing.
On 13.01.2022 14:40, Kenny McCormack wrote:
I have a situation where I need to convert a number (a 32 bit integer) to a >> 4 byte string - i.e., the internal representation of that 32 bit number as 4 >> consecutive bytes. This is so that I can pass (the address of) that string >> to a low-level routine that wants (basically) an "int *" value.
I managed to get it working, using the following function:
# This assumes 32 bit ints on a little-endian architecture.
# Call as: str = encode(number)
function encode(n, i,s) {
s = sprintf("%c",n)
for (i=1; i<4; i++)
s = s sprintf("%c",rshift(n,i*8))
return s
}
This works, but I'm wondering if there is a better/more efficient/cuter way >> to do it. Please discuss.
Well, the task has a few standard data splitting steps that you
implemented in a straightforward way. Effectively it's basically
fine and minimal, I'd say.
Just one thought one might want to take into consideration...
Recursive counterparts of iterative functions are typically clearer,
since they don't require explicit variables to be defined and assigned.
(And I presume that the function call overhead is insignificant here.)
Such a function may look as simple as
function encode(i,n) {
if (i>0) {
printf("%c",n)
encode(i-1,rshift(n,8))
}
}
and is called with an additional argument indicating the number of
octets e.g., encode(4, 0x41424344) or encode(4, 1094861636) to
produce "DCBA".
To hide function parameters like the "4" there's then often a wrapper function defined if one doesn't need to control the number of octets
function e(n) { encode(4,n) }
which of course "complicates" the matter again a bit (one may think).
But keeping that parameter allows also less function calls in case
you want to just extract, say, 2 or 3 octets from that number, as in encode(3, 0x41424344) which will produce the same result as the call encode(3, 0x00424344) .
Whether the clearness of recursion is "better" or "cuter" certainly
lies in the eye of the beholder. While I have to admit to rarely use recursion, in most cases I always admire these recursive solutions
once I've written them down and see how perfect they are as a concept.
Janis
Note, BTW, that I have verified that when you printf with %c, it only uses >> the low 8 bits of the number you pass in. So, you don't need to do any
"AND"ing.
Note, BTW, that I have verified that when you printf with %c, it only uses the low 8 bits of the number you pass in. So, you don't need to do any "AND"ing.
On 13.01.2022 14:40, Kenny McCormack wrote:
Note, BTW, that I have verified that when you printf with %c, it only uses >> the low 8 bits of the number you pass in. So, you don't need to do any
"AND"ing.
I also used that assumption in my code upthread but forgot to point
out that this is not reliable or is generally even not true because
that depends on the locale that you have set. Just two samples from
a Unix context...
I get it, but I am not too concerned about it. Since this method already assumes 32 bits and little-endian, I would just add to the list of assumptions: "No goofy locale settings". I.e., it works in the C locale.
Of course, I could make this whole problem go away by writing yet another extension lib to do it - but I was trying to avoid doing that.
On 14.01.2022 15:29, Kenny McCormack wrote:
I get it, but I am not too concerned about it. Since this method already assumes 32 bits and little-endian, I would just add to the list of assumptions: "No goofy locale settings". I.e., it works in the C locale.Fair enough. For others here it might be a fact to consider to not
get surprised.
Of course, I could make this whole problem go away by writing yet another extension lib to do it - but I was trying to avoid doing that.And that (with GNU Awk) would be the way to go.
Janis
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 27:41:24 |
Calls: | 10,390 |
Calls today: | 1 |
Files: | 14,064 |
Messages: | 6,417,072 |