Luka
Luka

Reputation: 1801

Javascript pack int?

var data1 = document.getElementById("data1").value;
var data2 = document.getElementById("data2").value;

Assuming the data2 contains some text and data1 contains only values from "0"-"255", how can I "combine" those?

Expected result: [single byte][data2]
What I got using data1+data2: [1-3 bytes][data2]

In C and C++:

unsigned char int_1 = 245;
unsigned char data[100];
//fill data with text but leave pos 0 empty
data[0] = int_1 ;

In PHP:

$data2 = pack("C", $data1).$data2;

Upvotes: 0

Views: 130

Answers (2)

axelduch
axelduch

Reputation: 10849

Well if I got it right, this is what you need

var finalData = String.fromCharCode(data1) + data2;

OR if you want to replace first char of data2

var finalData = String.fromCharCode(data1) + data2.substr(1);

EDIT: No need to use parseInt thanks to prinzhorn's comment

Upvotes: 2

T J
T J

Reputation: 43156

try parseInt(data1)+parseInt(data2);

Upvotes: 1

Related Questions