我试图将float直接转换为无符号整数而没有任何隐式转换数,(所以不是C风格或静态转换)只是将字节直接复制到另一个。在Windows Visual Studio 2015中,float和无符号整数的大小是相同的(4字节),所以我认为在这方面没有任何问题。 。 。我提出了一个解决方案,但必须有一个更好的方法来做我想要的。
unsigned int x = 3;
float y = 2.4565;
*reinterpret_cast<float*>(&x) = y;
这就是我想要的,并将X设置为1075656524。
如果有的话,我更喜欢跨平台的解决方案。我知道类型的大小可能因平台而异,所以这可能是不可能的。
编辑:为了澄清,我希望复制到unsigned int的float的所有字节都保持不变。存储在float中的每个位都应存储在无符号整数中。还有一个不使用memcpy的解决方案吗?我想避免使用已弃用的函数。
答案 0 :(得分:8)
我试图将一个浮点直接转换为一个无符号整数而没有任何隐式转换数,(所以不是C风格或静态转换)只是将字节直接复制到另一个
看起来你想要做的就是将位模式从一个内存位置复制到另一个内存位置。标准库函数<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta charset="UTF-8" />
<title>Facture </title>
<link
href="css/facture.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1>
FACTURE
</h1>
<h3>Facture</h3>
</header>
<table>
<thead>
<tr>
<th>Client</th>
</tr>
</thead>
<tbody>
<tr>
<td><h:outputText value="CodeL : #{printBean.code}"></h:outputText></td>
<td><h:outputText value="#{printBean.client}"></h:outputText></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td>Total: </td>
</tr>
</tfoot>
</table>
<footer>
<p>
Faculté des Sciences <a rel="nofollow"
href="mailto:vdap.vv@gmail.com">Courriel: vdap.vv@gmail.com </a>
</p>
</footer>
</body>
</html>
可用于此。只要意识到如果WARNING: Flying Saucer: No configuration files found in classpath using URL: xhtmlrenderer.conf, resorting to hard-coded fallback properties.
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at org.docx4j.org.xhtmlrenderer.css.parser.Lexer.zzRefill(Lexer.java:1634)
at org.docx4j.org.xhtmlrenderer.css.parser.Lexer.yylex(Lexer.java:1865)
at org.docx4j.org.xhtmlrenderer.css.parser.CSSParser.next(CSSParser.java:1807)
at org.docx4j.org.xhtmlrenderer.css.parser.CSSParser.la(CSSParser.java:1819)
at org.docx4j.org.xhtmlrenderer.css.parser.CSSParser.stylesheet(CSSParser.java:159)
at org.docx4j.org.xhtmlrenderer.css.parser.CSSParser.parseStylesheet(CSSParser.java:89)
at org.docx4j.org.xhtmlrenderer.context.StylesheetFactoryImpl.parse(StylesheetFactoryImpl.java:78)
at org.docx4j.org.xhtmlrenderer.context.StylesheetFactoryImpl.parse(StylesheetFactoryImpl.java:95)
at org.docx4j.org.xhtmlrenderer.context.StylesheetFactoryImpl.getStylesheet(StylesheetFactoryImpl.java:174)
at org.docx4j.org.xhtmlrenderer.context.StyleReference.readAndParseAll(StyleReference.java:123)
at org.docx4j.org.xhtmlrenderer.context.StyleReference.setDocumentContext(StyleReference.java:107)
at org.docx4j.org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:182)
at org.docx4j.org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:145)
at org.docx4j.org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:141)
at com.ismo.gestionabsence.presentation.PrintBean.createPDF(PrintBean.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
与memcpy
不同,那么所有这一切都没有实际意义。
sizeof(int)
更便携的解决方案是使用sizeof(float)
或unsigned int x = 3;
float y = 2.4565;
static_assert(sizeof(int) == sizeof(float), "Can't memcpy a float to an int");
memcpy(&x, &y);
的数组。
uint8_t
现在,您可以通过检查数组元素的值来检查位模式。