Converting NodeJS byte buffer [closed] - node.js

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to figure out how to convert NodeJS code like this:
const buffer = new Buffer(24);
offset = buffer.writeUInt32BE(this.a, offset);
offset = buffer.writeUInt32BE(this.b, offset);
offset = buffer.writeUInt8(this.c, offset);
offset = buffer.writeUInt16BE(d, e); 1 : 0, offset);
buffer.writeInt8(this.f, offset);
to Go.
I figured I could use
buffer := make([]byte, 24)
buffer[0] = a
buffer[2] = b
but this is not working
is there a recommended way to do something like this with Go?

You should use binary.ByteOrder.
So in your case, using Big Endian, something like :
package main
import (
"encoding/binary"
)
func main() {
buffer := make([]byte, 24)
// Uint32
binary.BigEndian.PutUint32(buffer, 1)
binary.BigEndian.PutUint32(buffer[4:], 2)
// Uint8
buffer[8] = 3
// Uint16
binary.BigEndian.PutUint16(buffer[9:], 4)
// Uint8
buffer[13] = 5
}

Related

How to access a value from struct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
I'm trying to get the "Entries" from the below struct. I'm not sure how to get it. Need help here.
https://pkg.go.dev/github.com/lrh3321/ipset-go#v0.0.0-20221207025854-43e1992fa544#List
func List(setname string) (*Sets, error)
type Sets struct {
Nfgenmsg *nl.Nfgenmsg
Protocol uint8
ProtocolMinVersion uint8
Revision uint8
Family uint8
Flags uint8
SetName string
TypeName string
Comment string
MarkMask uint32
IPFrom net.IP
IPTo net.IP
PortFrom uint16
PortTo uint16
Size uint32
HashSize uint32
NumEntries uint32
MaxElements uint32
References uint32
SizeInMemory uint32
CadtFlags uint32
Timeout *uint32
LineNo uint32
Entries []Entry
}
out, err1 := ipset.List(cntr)
if err1 != nil {
log.Errorf("Error when fetching the list %s", err1)
}
log.Infof("[Accounting] printing the ipset list %s", *out. Entries)
I get the below error
invalid indirect of out.Entries (type []ipset.Entry)

Wrong u128 multiplication in Rust? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Before I open bug, I want to check what is going on here.
I'm porting this C-code here to Rust:
unsigned __int128 r = (unsigned __int128)a * (unsigned __int128)b;
easy enough (I thought):
let r = (a as u128) * (b as u128);
Now with this input parameters I get a different multiplication result in C and Rust:
(0x56eaa5f5f650a9e3 as u128) * (0xa0cf24341e75bda9 as u128)
The results are different in Rust and C:
Rust: 0x3698fbc09d2c5b15e8889b1b676bbddb
C: 0x3698fbc0f417010bded944fe676bbddb
^^^^^^^^^^^^^^^^
I cross-checked the result, and got the same result as the C code.
Am I missing something?
=== context information added:
It is this function from xmr-stak (https://github.com/fireice-uk/xmr-stak) thas is behaving differently:
static inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi)
{
unsigned __int128 r = (unsigned __int128)a * (unsigned __int128)b;
*hi = r >> 64;
return (uint64_t)r;
}
Regardless if the C implementation is wrong, I have to recreate the exact computation in Rust, because this is needed for a hash computation.
I looks like you must have a made a typo in either language:
>>> hex(0x3698fbc09d2c5b15e8889b1b676bbddb//0x56eaa5f5f650a9e3)
'0xa0cf24341e75bda9' # what your Rust code uses
>>> hex(0x3698fbc0f417010bded944fe676bbddb//0x56eaa5f5f650a9e3)
'0xa0cf24351e75bda9' # what your online calculator uses
^
Classical case of off-by-0x100000000 error :)

Converting int to string without printing [duplicate]

This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 8 years ago.
I want to convert an int to a string without printing anything on my screen. For now I used sprintf, but this also printed the int to my screen.
Also itoa is not supported by my compiler so I can't use that either.
I assume You use ANSI C.
You cannot use itoa because it's not a standard function.
sprintf or snprintf is dedicated to that.
Since You do not want to use sprintf, make your own itoa instead:
#include <stdio.h>
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '\0';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
}while(i);
return b;
}
original answer: here

Easy tool to convert data to different format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm studying statistics and programming on my own. What kind of C or Python program will solve the following problem in Linux?
I have a text (maybe CSV) file of the form
pcb138 pcb180 pcb52 pcb118 pcb
1,46 ,738 ,532 ,72 19,9959
,64 ,664 ,03 ,236 6,0996
3,29 1,15 ,134 1,54 24,9655
3,94 1,33 ,466 1,94 37,4436
...
32,3 31,5 1,8 8,49 318,7461
Now I would like to convert those to another format that another program understands. Namely, a text file of the form
pcb138=[1.46,0.64,3.94,...,32.3]
pcb180=[0.738,0.664, 1.15,1.33,...,31.5]
pbc52=[0.532, 0.03, 0.134, 0.466, ...,1.8]
pbc118=[0.72, 0.236, 0.154, 1.94, ...,8.49]
pbc=[19.9959, 6.0996, 24.9655, 37.4436, ...,318.7461]
Write a C program using a 2 dimensional array and strtok() to do the parsing
http://www.cplusplus.com/reference/cstring/strtok/
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}

What programing language is this? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Someone know what kind of language used below:
String^ fileName = "C:\\Test1.txt";
array<Byte>^ Array = gcnew array<Byte>(512);
try
{
FileStream^ fs = File::OpenRead(fileName);
fs->Read(Array, 0, 512);fs->Close();
}
catch (...)
{
MessageBox::Show("Disk error");
Application::Exit();
}
and another example of that language:
int RotateLeft3 (int number)
{
if ( ( number & 0x20000000 ) == 0x20000000 )
{
number <<= 3;number |= 1;
}
else
number <<= 3;
return number;
}
Its C++ in .NET. You can tell by the use of ^ as pointer instead of *
This is C++/CLI, in other words the C++ variant that runs on top of the .Net CLR.
On no account should this be confused with native C++.
It looks like managed c++ from Microsoft.

Resources