c# text to int64 representation
up vote
0
down vote
favorite
I'm trying to convert the string
"Eureka"
into its UTF-8 Int64
representation.
I'm trying the following code :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
// this fails because I have 6 bytes, not 8 (as required for Int64)
Int64 m = BitConverter.ToInt64(bytes, 0);
byte decodeBites = BitConverter.GetBytes(m);
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Now, because my string is too short, I don't have the right number of bytes and it fails. If I add 2 chars, it works, but then I don't have the desired string in decodeMessage
... Pretty sure I need some conversion trick to remove the trailing "0" bytes after the conversion, but none of my attempts work. Any help would be much appreciated!!
UPDATE
The goal is REALLY to have the integer representation of "Euraka", not "Eureka" then trim at the end.... The goals is to use the RSA-method on the obtained Int64
, so at the other end, people won't know they have to trim anything...!
c# byte bytestring int64
add a comment |
up vote
0
down vote
favorite
I'm trying to convert the string
"Eureka"
into its UTF-8 Int64
representation.
I'm trying the following code :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
// this fails because I have 6 bytes, not 8 (as required for Int64)
Int64 m = BitConverter.ToInt64(bytes, 0);
byte decodeBites = BitConverter.GetBytes(m);
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Now, because my string is too short, I don't have the right number of bytes and it fails. If I add 2 chars, it works, but then I don't have the desired string in decodeMessage
... Pretty sure I need some conversion trick to remove the trailing "0" bytes after the conversion, but none of my attempts work. Any help would be much appreciated!!
UPDATE
The goal is REALLY to have the integer representation of "Euraka", not "Eureka" then trim at the end.... The goals is to use the RSA-method on the obtained Int64
, so at the other end, people won't know they have to trim anything...!
c# byte bytestring int64
1
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
If you are going to useBitConverter.ToInt64
then you have to pad with0
the bytes array.
– Samvel Petrosov
Nov 21 at 13:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to convert the string
"Eureka"
into its UTF-8 Int64
representation.
I'm trying the following code :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
// this fails because I have 6 bytes, not 8 (as required for Int64)
Int64 m = BitConverter.ToInt64(bytes, 0);
byte decodeBites = BitConverter.GetBytes(m);
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Now, because my string is too short, I don't have the right number of bytes and it fails. If I add 2 chars, it works, but then I don't have the desired string in decodeMessage
... Pretty sure I need some conversion trick to remove the trailing "0" bytes after the conversion, but none of my attempts work. Any help would be much appreciated!!
UPDATE
The goal is REALLY to have the integer representation of "Euraka", not "Eureka" then trim at the end.... The goals is to use the RSA-method on the obtained Int64
, so at the other end, people won't know they have to trim anything...!
c# byte bytestring int64
I'm trying to convert the string
"Eureka"
into its UTF-8 Int64
representation.
I'm trying the following code :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
// this fails because I have 6 bytes, not 8 (as required for Int64)
Int64 m = BitConverter.ToInt64(bytes, 0);
byte decodeBites = BitConverter.GetBytes(m);
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Now, because my string is too short, I don't have the right number of bytes and it fails. If I add 2 chars, it works, but then I don't have the desired string in decodeMessage
... Pretty sure I need some conversion trick to remove the trailing "0" bytes after the conversion, but none of my attempts work. Any help would be much appreciated!!
UPDATE
The goal is REALLY to have the integer representation of "Euraka", not "Eureka" then trim at the end.... The goals is to use the RSA-method on the obtained Int64
, so at the other end, people won't know they have to trim anything...!
c# byte bytestring int64
c# byte bytestring int64
edited Nov 21 at 13:48
asked Nov 21 at 13:36
neggenbe
619726
619726
1
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
If you are going to useBitConverter.ToInt64
then you have to pad with0
the bytes array.
– Samvel Petrosov
Nov 21 at 13:54
add a comment |
1
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
If you are going to useBitConverter.ToInt64
then you have to pad with0
the bytes array.
– Samvel Petrosov
Nov 21 at 13:54
1
1
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
If you are going to use
BitConverter.ToInt64
then you have to pad with 0
the bytes array.– Samvel Petrosov
Nov 21 at 13:54
If you are going to use
BitConverter.ToInt64
then you have to pad with 0
the bytes array.– Samvel Petrosov
Nov 21 at 13:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Let's pad the array with 0
(to ensure the array contain of 8
bytes) and trim the string from :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
byte pad = new byte[bytes.Length < sizeof(Int64) ? sizeof(Int64) - bytes.Length : 0];
// Concat(new byte[...]) - ensure m contains 8 bytes
Int64 m = BitConverter.ToInt64(
bytes.Concat(pad).ToArray(),
0);
byte decodeBites = BitConverter.GetBytes(m);
// Since we have 8 bytes always, we have to get rid of tail ''
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites).TrimEnd('');
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Edit: Since Int64
has 8 and "Eureka"
is encoded in 6 only, will-nilly you have to get rid of 2 zero bytes. If you don't want Trim
you can Skip
them e.g.
byte decodeBites = BitConverter
.GetBytes(m)
.TakeWhile(b => b != 0)
.ToArray();
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
@neggenbe What isc.f. edits
supposed to mean?
– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert abyte
to an int value... I'd think something likesum(256^i*byte[i]
, no??
– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Let's pad the array with 0
(to ensure the array contain of 8
bytes) and trim the string from :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
byte pad = new byte[bytes.Length < sizeof(Int64) ? sizeof(Int64) - bytes.Length : 0];
// Concat(new byte[...]) - ensure m contains 8 bytes
Int64 m = BitConverter.ToInt64(
bytes.Concat(pad).ToArray(),
0);
byte decodeBites = BitConverter.GetBytes(m);
// Since we have 8 bytes always, we have to get rid of tail ''
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites).TrimEnd('');
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Edit: Since Int64
has 8 and "Eureka"
is encoded in 6 only, will-nilly you have to get rid of 2 zero bytes. If you don't want Trim
you can Skip
them e.g.
byte decodeBites = BitConverter
.GetBytes(m)
.TakeWhile(b => b != 0)
.ToArray();
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
@neggenbe What isc.f. edits
supposed to mean?
– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert abyte
to an int value... I'd think something likesum(256^i*byte[i]
, no??
– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
add a comment |
up vote
3
down vote
accepted
Let's pad the array with 0
(to ensure the array contain of 8
bytes) and trim the string from :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
byte pad = new byte[bytes.Length < sizeof(Int64) ? sizeof(Int64) - bytes.Length : 0];
// Concat(new byte[...]) - ensure m contains 8 bytes
Int64 m = BitConverter.ToInt64(
bytes.Concat(pad).ToArray(),
0);
byte decodeBites = BitConverter.GetBytes(m);
// Since we have 8 bytes always, we have to get rid of tail ''
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites).TrimEnd('');
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Edit: Since Int64
has 8 and "Eureka"
is encoded in 6 only, will-nilly you have to get rid of 2 zero bytes. If you don't want Trim
you can Skip
them e.g.
byte decodeBites = BitConverter
.GetBytes(m)
.TakeWhile(b => b != 0)
.ToArray();
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
@neggenbe What isc.f. edits
supposed to mean?
– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert abyte
to an int value... I'd think something likesum(256^i*byte[i]
, no??
– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Let's pad the array with 0
(to ensure the array contain of 8
bytes) and trim the string from :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
byte pad = new byte[bytes.Length < sizeof(Int64) ? sizeof(Int64) - bytes.Length : 0];
// Concat(new byte[...]) - ensure m contains 8 bytes
Int64 m = BitConverter.ToInt64(
bytes.Concat(pad).ToArray(),
0);
byte decodeBites = BitConverter.GetBytes(m);
// Since we have 8 bytes always, we have to get rid of tail ''
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites).TrimEnd('');
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Edit: Since Int64
has 8 and "Eureka"
is encoded in 6 only, will-nilly you have to get rid of 2 zero bytes. If you don't want Trim
you can Skip
them e.g.
byte decodeBites = BitConverter
.GetBytes(m)
.TakeWhile(b => b != 0)
.ToArray();
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
Let's pad the array with 0
(to ensure the array contain of 8
bytes) and trim the string from :
string message = "Eureka"; // if I use "Eureka" it works...
byte bytes = System.Text.Encoding.UTF8.GetBytes(message);
byte pad = new byte[bytes.Length < sizeof(Int64) ? sizeof(Int64) - bytes.Length : 0];
// Concat(new byte[...]) - ensure m contains 8 bytes
Int64 m = BitConverter.ToInt64(
bytes.Concat(pad).ToArray(),
0);
byte decodeBites = BitConverter.GetBytes(m);
// Since we have 8 bytes always, we have to get rid of tail ''
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites).TrimEnd('');
if (!decodeMessage.Equals(message)) {
Console.WriteLine("Message mis-match!!!");
}
Edit: Since Int64
has 8 and "Eureka"
is encoded in 6 only, will-nilly you have to get rid of 2 zero bytes. If you don't want Trim
you can Skip
them e.g.
byte decodeBites = BitConverter
.GetBytes(m)
.TakeWhile(b => b != 0)
.ToArray();
string decodeMessage = System.Text.Encoding.UTF8.GetString(decodeBites);
edited Nov 21 at 13:53
answered Nov 21 at 13:43
Dmitry Bychenko
104k992131
104k992131
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
@neggenbe What isc.f. edits
supposed to mean?
– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert abyte
to an int value... I'd think something likesum(256^i*byte[i]
, no??
– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
add a comment |
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
@neggenbe What isc.f. edits
supposed to mean?
– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert abyte
to an int value... I'd think something likesum(256^i*byte[i]
, no??
– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
thx - c.f. edits!!
– neggenbe
Nov 21 at 13:45
1
1
@neggenbe What is
c.f. edits
supposed to mean?– Rand Random
Nov 21 at 14:02
@neggenbe What is
c.f. edits
supposed to mean?– Rand Random
Nov 21 at 14:02
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert a
byte
to an int value... I'd think something like sum(256^i*byte[i]
, no??– neggenbe
Nov 21 at 15:04
Again, what I am interested in is "what is the int value of bytes" ?? In other words, what is the formulae to convert a
byte
to an int value... I'd think something like sum(256^i*byte[i]
, no??– neggenbe
Nov 21 at 15:04
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
Ok, I now got your solution, took me some time - you remove the empty "zero" bytes in the array. Works great, cheers!!
– neggenbe
Nov 23 at 7:34
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413275%2fc-sharp-text-to-int64-representation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That isn't ASCII, consider removing that from your question.
– Crowcoder
Nov 21 at 13:48
Changed to UTF-8 already...
– neggenbe
Nov 21 at 13:49
If you are going to use
BitConverter.ToInt64
then you have to pad with0
the bytes array.– Samvel Petrosov
Nov 21 at 13:54