How to find the number of bytes of an integer value stored in a integer variable

Himanshu Singh

I need to pass an argument in a function to connect to a server which has to contain the xml(which I am passing through a string variable). But the server assumes that first 4 bytes of the data stream which I am sending is the length of the xml (in bytes). I have found out the length of the number of byte in the string by

int strlen= System.Text.ASCIIEncoding.Unicode.GetByteCount(ngconnect);

Now the next thing is I need to pass the data to the server which has to contain the data as

First 4 bytes as the length of the string Then the actual data stream.

The code which I have written is

byte[] outStream = System.Text.Encoding.ASCII.GetBytes(strlen+ngconnect);
            serverStream.Write(outStream, 0, outStream.Length);

Which I dont think is correct. So please do tell me how should I append the length of the string in the argument such that it is exactly of 4 bytes.

JoaoFSA

The int in c# is a 32bit number so its already 4bytes, you just need to convert it to bytes

byte[] strlenBytes = BitConverter.GetBytes(strlen);

and then transmit the info to the server in parts:

serverStream.Write(strlenBytes , 0, strlenBytes .Length);
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(ngconnect);
serverStream.Write(outStream, 0, outStream.Length);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to declare a variable based on value of another Integer?

分類Dev

How to find the minimal numpy dtype to store a maximum integer value?

分類Dev

assign non-integer value to integer variable in C

分類Dev

convert an array of bytes into an integer

分類Dev

How to check if value is logically integer?

分類Dev

trying to find the value is numeric or integer from string

分類Dev

Trying to switch a direct integer value with a variable in swift

分類Dev

How to distribute an integer number across bins by probability

分類Dev

How to convert floating point number to integer?

分類Dev

How to tween an integer variable using TweenMax?

分類Dev

How to tween an integer variable using TweenMax?

分類Dev

How to force the input of a variable to be an integer in Ruby

分類Dev

How to fix the Integer literal '2147483648' overflows when stored into 'Int' exception?

分類Dev

How get integer value from a enum in Rails?

分類Dev

How to get an integer value from Entry in Tkinter?

分類Dev

Stored Procedure Not Accepting Integer Parameter

分類Dev

Integer array of variable length

分類Dev

Different results between an integer variable and casting an integer to an integer

分類Dev

Sort integer by absolute value

分類Dev

Changing a value of an integer in a function

分類Dev

Converting dictionary value to integer

分類Dev

In Python, how would you check if a number is one of the integer types?

分類Dev

how to convert timedelta64 to number of days integer?

分類Dev

How to detect index of a specific number in a std::integer_sequence?

分類Dev

How to divide an unknown integer into a given number of even parts using Python

分類Dev

How to add together an array representation of an integer with different number of digits?

分類Dev

How do you get an integer number for an item in group in SQL?

分類Dev

How to find the minimum positive integer in a list of lists in O(n)?

分類Dev

How do you iterate over an integer variable in Jinja?

Related 関連記事

  1. 1

    How to declare a variable based on value of another Integer?

  2. 2

    How to find the minimal numpy dtype to store a maximum integer value?

  3. 3

    assign non-integer value to integer variable in C

  4. 4

    convert an array of bytes into an integer

  5. 5

    How to check if value is logically integer?

  6. 6

    trying to find the value is numeric or integer from string

  7. 7

    Trying to switch a direct integer value with a variable in swift

  8. 8

    How to distribute an integer number across bins by probability

  9. 9

    How to convert floating point number to integer?

  10. 10

    How to tween an integer variable using TweenMax?

  11. 11

    How to tween an integer variable using TweenMax?

  12. 12

    How to force the input of a variable to be an integer in Ruby

  13. 13

    How to fix the Integer literal '2147483648' overflows when stored into 'Int' exception?

  14. 14

    How get integer value from a enum in Rails?

  15. 15

    How to get an integer value from Entry in Tkinter?

  16. 16

    Stored Procedure Not Accepting Integer Parameter

  17. 17

    Integer array of variable length

  18. 18

    Different results between an integer variable and casting an integer to an integer

  19. 19

    Sort integer by absolute value

  20. 20

    Changing a value of an integer in a function

  21. 21

    Converting dictionary value to integer

  22. 22

    In Python, how would you check if a number is one of the integer types?

  23. 23

    how to convert timedelta64 to number of days integer?

  24. 24

    How to detect index of a specific number in a std::integer_sequence?

  25. 25

    How to divide an unknown integer into a given number of even parts using Python

  26. 26

    How to add together an array representation of an integer with different number of digits?

  27. 27

    How do you get an integer number for an item in group in SQL?

  28. 28

    How to find the minimum positive integer in a list of lists in O(n)?

  29. 29

    How do you iterate over an integer variable in Jinja?

ホットタグ

アーカイブ