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...!










share|improve this question




















  • 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 with 0 the bytes array.
    – Samvel Petrosov
    Nov 21 at 13:54















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...!










share|improve this question




















  • 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 with 0 the bytes array.
    – Samvel Petrosov
    Nov 21 at 13:54













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...!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 use BitConverter.ToInt64 then you have to pad with 0 the bytes array.
    – Samvel Petrosov
    Nov 21 at 13:54














  • 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 with 0 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












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);





share|improve this answer























  • thx - c.f. edits!!
    – neggenbe
    Nov 21 at 13:45






  • 1




    @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










  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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);





share|improve this answer























  • thx - c.f. edits!!
    – neggenbe
    Nov 21 at 13:45






  • 1




    @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










  • 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















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);





share|improve this answer























  • thx - c.f. edits!!
    – neggenbe
    Nov 21 at 13:45






  • 1




    @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










  • 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













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);





share|improve this answer














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);






share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • 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






  • 1




    @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










  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks